/** * Creates a new {@link ContainerRequest} for the given input. * * @param request * @param containerResponseWriter * @param securityContext * @return */ @Nonnull protected ContainerRequest createContainerRequest(@Nonnull RequestT request, @Nonnull ContainerResponseWriter containerResponseWriter, @Nonnull SecurityContext securityContext) { requireNonNull(request, "request may not be null"); URI baseUri = request.getBaseUri(); URI requestUri = requireNonNull(request.getRequestUri(), "request.getRequestUri() may not be null"); String httpMethod = requireNonNull(request.getHttpMethod(), "request.getHttpMethod() may not be null"); InputStream entityStream = requireNonNull(request.getEntityStream(), "request.getEntityStream() may not be null"); Map<String, List<String>> headers = requireNonNull(request.getHeaders(), "request.getHeaderParams() may not be null"); requireNonNull(containerResponseWriter, "containerResponseWriter may not be null"); requireNonNull(securityContext, "securityContext may not be null"); ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, httpMethod, securityContext, new MapPropertiesDelegate()); requestContext.setEntityStream(entityStream); requestContext.getHeaders().putAll(headers); requestContext.setWriter(containerResponseWriter); return requestContext; }
@Test public void createContainerRequest_RequestAndContainerResponseWriterAndSecurityContextGiven_ShouldCreateContainerRequestUsingValues() { URI baseUri = URI.create("/"); URI requestUri = URI.create("/entity"); String httpMethod = "POST"; ByteArrayInputStream entityStream = new ByteArrayInputStream(new byte[0]); MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); headers.add("hk0", "hv0_0"); headers.add("hk0", "hv0_1"); headers.add("hk1", "hv1_0"); JRestlessContainerRequest request = createRequest(baseUri, requestUri, httpMethod, entityStream, headers); ContainerResponseWriter containerResponseWriter = mock(ContainerResponseWriter.class); SecurityContext securityContext = mock(SecurityContext.class); ContainerRequest containerRequest = container.createContainerRequest(request, containerResponseWriter, securityContext); assertEquals(baseUri, containerRequest.getBaseUri()); assertEquals(requestUri, containerRequest.getRequestUri()); assertEquals(httpMethod, containerRequest.getMethod()); assertSame(entityStream, containerRequest.getEntityStream()); assertEquals(headers, containerRequest.getHeaders()); assertSame(containerResponseWriter, containerRequest.getResponseWriter()); assertSame(securityContext, containerRequest.getSecurityContext()); }
private void flushResponse(RequestEvent requestEvent) { try { ContainerResponseWriter writer = requestEvent.getContainerRequest().getResponseWriter(); writer.writeResponseStatusAndHeaders(0, requestEvent.getContainerResponse()).flush(); } catch (Exception e) { // no op } }
@Test public void createContainerRequest_NoRequestGiven_ShouldThrowNpe() { assertThrows(NullPointerException.class, () -> container.createContainerRequest(null, mock(ContainerResponseWriter.class), mock(SecurityContext.class))); }
@Test public void createContainerRequest_NoSecurityContext_ShouldThrowNpe() { assertThrows(NullPointerException.class, () -> container.createContainerRequest(createAnyRequest(), mock(ContainerResponseWriter.class), null)); }
public void createContainerRequest_NoRequestBaseUriGiven_ShouldPass() { JRestlessContainerRequest request = createAnyRequest(); when(request.getBaseUri()).thenReturn(null); container.createContainerRequest(request, mock(ContainerResponseWriter.class), mock(SecurityContext.class)); }
@Test public void createContainerRequest_NoRequestRequestUriGiven_ShouldThrowNpe() { JRestlessContainerRequest request = createAnyRequest(); when(request.getRequestUri()).thenReturn(null); assertThrows(NullPointerException.class, () -> container.createContainerRequest(request, mock(ContainerResponseWriter.class), mock(SecurityContext.class))); }
@Test public void createContainerRequest_NoRequestHttpMethodGiven_ShouldThrowNpe() { JRestlessContainerRequest request = createAnyRequest(); when(request.getHttpMethod()).thenReturn(null); assertThrows(NullPointerException.class, () -> container.createContainerRequest(request, mock(ContainerResponseWriter.class), mock(SecurityContext.class))); }
@Test public void createContainerRequest_NoRequestEntityStreamGiven_ShouldThrowNpe() { JRestlessContainerRequest request = createAnyRequest(); when(request.getEntityStream()).thenReturn(null); assertThrows(NullPointerException.class, () -> container.createContainerRequest(request, mock(ContainerResponseWriter.class), mock(SecurityContext.class))); }
@Test public void createContainerRequest_NoRequestHeadersGiven_ShouldThrowNpe() { JRestlessContainerRequest request = createAnyRequest(); when(request.getHeaders()).thenReturn(null); assertThrows(NullPointerException.class, () -> container.createContainerRequest(request, mock(ContainerResponseWriter.class), mock(SecurityContext.class))); }
/** * <p>setWriter.</p> * * @param responseWriter a {@link org.glassfish.jersey.server.spi.ContainerResponseWriter} object. */ public static void setWriter(ContainerResponseWriter responseWriter) { getRequest().setWriter(responseWriter); }
/** * <p>getResponseWriter.</p> * * @return a {@link org.glassfish.jersey.server.spi.ContainerResponseWriter} object. */ public static ContainerResponseWriter getResponseWriter() { return getRequest().getResponseWriter(); }