@Test public void writeResponseStatusAndHeaders_ContextHeaderAndStatusGiven_ShouldUpdateResponseStatusAndHeaders() { MultivaluedMap<String, String> actualHeaders = new MultivaluedHashMap<>(); actualHeaders.add("header0", "value0_0"); actualHeaders.add("header0", "value0_1"); actualHeaders.add("header1", "value1_0"); MultivaluedMap<String, String> expectedHeaders = new MultivaluedHashMap<>(); expectedHeaders.add("header0", "value0_0"); expectedHeaders.add("header0", "value0_1"); expectedHeaders.add("header1", "value1_0"); ContainerResponse context = mock(ContainerResponse.class); when(context.getStatusInfo()).thenReturn(Status.CONFLICT); when(context.getStringHeaders()).thenReturn(actualHeaders); containerResponseWriter.writeResponseStatusAndHeaders(-1, context); assertEquals(Status.CONFLICT, response.getStatusType()); assertEquals(expectedHeaders, response.getHeaders()); }
@Test public void listOwnerTest() { org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class); owner.persist(); ContainerResponse response = call(request("/owners/", HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserializeCollection( getContent(response), java.util.List.class, org.minnal.examples.petclinic.domain.Owner.class) .size(), (int) org.minnal.examples.petclinic.domain.Owner .count()); }
@Test public void listOwnerPetTest() { org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class); owner.persist(); org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class); owner.collection("pets").add(pet); owner.persist(); ContainerResponse response = call(request( "/owners/" + owner.getId() + "/pets/", HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserializeCollection(getContent(response), java.util.List.class, org.minnal.examples.petclinic.domain.Pet.class) .size(), owner.getPets().size()); }
@Test public void readOwnerPetTest() { org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class); owner.persist(); org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class); owner.collection("pets").add(pet); owner.persist(); ContainerResponse response = call(request( "/owners/" + owner.getId() + "/pets/" + pet.getId(), HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserialize(getContent(response), org.minnal.examples.petclinic.domain.Pet.class) .getId(), pet.getId()); }
@Test public void updateOwnerPetTest() { org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class); owner.persist(); org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class); owner.collection("pets").add(pet); owner.persist(); org.minnal.examples.petclinic.domain.Pet modifiedpet = createDomain( org.minnal.examples.petclinic.domain.Pet.class, 1); ContainerResponse response = call(request( "/owners/" + owner.getId() + "/pets/" + pet.getId(), HttpMethod.PUT, serialize(modifiedpet))); assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); pet.merge(); assertTrue(compare(modifiedpet, pet, 1)); }
@Test public void deleteOwnerPetTest() { org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class); owner.persist(); org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class); owner.collection("pets").add(pet); owner.persist(); ContainerResponse response = call(request( "/owners/" + owner.getId() + "/pets/" + pet.getId(), HttpMethod.DELETE)); assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); response = call(request("/owners/" + owner.getId() + "/pets/" + pet.getId(), HttpMethod.GET, serialize(pet))); assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); }
@Test public void listOwnerPetVisitTest() { org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class); owner.persist(); org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class); owner.collection("pets").add(pet); owner.persist(); org.minnal.examples.petclinic.domain.Visit visit = createDomain(org.minnal.examples.petclinic.domain.Visit.class); pet.collection("visits").add(visit); pet.persist(); ContainerResponse response = call(request( "/owners/" + owner.getId() + "/pets/" + pet.getId() + "/visits/", HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserializeCollection( getContent(response), java.util.List.class, org.minnal.examples.petclinic.domain.Visit.class) .size(), pet.getVisits().size()); }
@Test public void readOwnerPetVisitTest() { org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class); owner.persist(); org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class); owner.collection("pets").add(pet); owner.persist(); org.minnal.examples.petclinic.domain.Visit visit = createDomain(org.minnal.examples.petclinic.domain.Visit.class); pet.collection("visits").add(visit); pet.persist(); ContainerResponse response = call(request( "/owners/" + owner.getId() + "/pets/" + pet.getId() + "/visits/" + visit.getId(), HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserialize( getContent(response), org.minnal.examples.petclinic.domain.Visit.class) .getId(), visit.getId()); }
@Test public void updateOwnerPetVisitTest() { org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class); owner.persist(); org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class); owner.collection("pets").add(pet); owner.persist(); org.minnal.examples.petclinic.domain.Visit visit = createDomain(org.minnal.examples.petclinic.domain.Visit.class); pet.collection("visits").add(visit); pet.persist(); org.minnal.examples.petclinic.domain.Visit modifiedvisit = createDomain( org.minnal.examples.petclinic.domain.Visit.class, 1); ContainerResponse response = call(request( "/owners/" + owner.getId() + "/pets/" + pet.getId() + "/visits/" + visit.getId(), HttpMethod.PUT, serialize(modifiedvisit))); assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); visit.merge(); assertTrue(compare(modifiedvisit, visit, 1)); }
@Test public void deleteOwnerPetVisitTest() { org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class); owner.persist(); org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class); owner.collection("pets").add(pet); owner.persist(); org.minnal.examples.petclinic.domain.Visit visit = createDomain(org.minnal.examples.petclinic.domain.Visit.class); pet.collection("visits").add(visit); pet.persist(); ContainerResponse response = call(request( "/owners/" + owner.getId() + "/pets/" + pet.getId() + "/visits/" + visit.getId(), HttpMethod.DELETE)); assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); response = call(request("/owners/" + owner.getId() + "/pets/" + pet.getId() + "/visits/" + visit.getId(), HttpMethod.GET, serialize(visit))); assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); }
@Test public void listVetSpecialtyTest() { org.minnal.examples.petclinic.domain.Vet vet = createDomain(org.minnal.examples.petclinic.domain.Vet.class); vet.persist(); org.minnal.examples.petclinic.domain.Specialty specialty = createDomain(org.minnal.examples.petclinic.domain.Specialty.class); vet.collection("specialties").add(specialty); vet.persist(); ContainerResponse response = call(request( "/vets/" + vet.getId() + "/specialties/", HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserializeCollection( getContent(response), java.util.List.class, org.minnal.examples.petclinic.domain.Specialty.class) .size(), vet.getSpecialties().size()); }
@Test public void readVetSpecialtyTest() { org.minnal.examples.petclinic.domain.Vet vet = createDomain(org.minnal.examples.petclinic.domain.Vet.class); vet.persist(); org.minnal.examples.petclinic.domain.Specialty specialty = createDomain(org.minnal.examples.petclinic.domain.Specialty.class); vet.collection("specialties").add(specialty); vet.persist(); ContainerResponse response = call(request( "/vets/" + vet.getId() + "/specialties/" + specialty.getId(), HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserialize( getContent(response), org.minnal.examples.petclinic.domain.Specialty.class) .getId(), specialty.getId()); }
@Test public void updateVetSpecialtyTest() { org.minnal.examples.petclinic.domain.Vet vet = createDomain(org.minnal.examples.petclinic.domain.Vet.class); vet.persist(); org.minnal.examples.petclinic.domain.Specialty specialty = createDomain(org.minnal.examples.petclinic.domain.Specialty.class); vet.collection("specialties").add(specialty); vet.persist(); org.minnal.examples.petclinic.domain.Specialty modifiedspecialty = createDomain( org.minnal.examples.petclinic.domain.Specialty.class, 1); ContainerResponse response = call(request( "/vets/" + vet.getId() + "/specialties/" + specialty.getId(), HttpMethod.PUT, serialize(modifiedspecialty))); assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); specialty.merge(); assertTrue(compare(modifiedspecialty, specialty, 1)); }
@Test public void deleteVetSpecialtyTest() { org.minnal.examples.petclinic.domain.Vet vet = createDomain(org.minnal.examples.petclinic.domain.Vet.class); vet.persist(); org.minnal.examples.petclinic.domain.Specialty specialty = createDomain(org.minnal.examples.petclinic.domain.Specialty.class); vet.collection("specialties").add(specialty); vet.persist(); ContainerResponse response = call(request( "/vets/" + vet.getId() + "/specialties/" + specialty.getId(), HttpMethod.DELETE)); assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); response = call(request("/vets/" + vet.getId() + "/specialties/" + specialty.getId(), HttpMethod.GET, serialize(specialty))); assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); }
@Test public void listPetTypeTest() { org.minnal.examples.petclinic.domain.PetType petType = createDomain(org.minnal.examples.petclinic.domain.PetType.class); petType.persist(); ContainerResponse response = call(request("/pet_types/", HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserializeCollection( getContent(response), java.util.List.class, org.minnal.examples.petclinic.domain.PetType.class) .size(), (int) org.minnal.examples.petclinic.domain.PetType .count()); }
@Test public void listOrderPaymentTest() { org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class); order.persist(); org.minnal.examples.oms.domain.Payment payment = createDomain(org.minnal.examples.oms.domain.Payment.class); order.collection("payments").add(payment); order.persist(); ContainerResponse response = call(request( "/orders/" + order.getId() + "/payments/", HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserializeCollection(getContent(response), java.util.List.class, org.minnal.examples.oms.domain.Payment.class) .size(), order.getPayments().size()); }
@Test public void readOrderPaymentTest() { org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class); order.persist(); org.minnal.examples.oms.domain.Payment payment = createDomain(org.minnal.examples.oms.domain.Payment.class); order.collection("payments").add(payment); order.persist(); ContainerResponse response = call(request( "/orders/" + order.getId() + "/payments/" + payment.getId(), HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserialize(getContent(response), org.minnal.examples.oms.domain.Payment.class) .getId(), payment.getId()); }
@Test public void updateOrderPaymentTest() { org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class); order.persist(); org.minnal.examples.oms.domain.Payment payment = createDomain(org.minnal.examples.oms.domain.Payment.class); order.collection("payments").add(payment); order.persist(); org.minnal.examples.oms.domain.Payment modifiedpayment = createDomain( org.minnal.examples.oms.domain.Payment.class, 1); ContainerResponse response = call(request( "/orders/" + order.getId() + "/payments/" + payment.getId(), HttpMethod.PUT, serialize(modifiedpayment))); assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); payment.merge(); assertTrue(compare(modifiedpayment, payment, 1)); }
@Test public void deleteOrderPaymentTest() { org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class); order.persist(); org.minnal.examples.oms.domain.Payment payment = createDomain(org.minnal.examples.oms.domain.Payment.class); order.collection("payments").add(payment); order.persist(); ContainerResponse response = call(request( "/orders/" + order.getId() + "/payments/" + payment.getId(), HttpMethod.DELETE)); assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); response = call(request("/orders/" + order.getId() + "/payments/" + payment.getId(), HttpMethod.GET, serialize(payment))); assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); }
@Test public void listOrderOrderItemTest() { org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class); order.persist(); org.minnal.examples.oms.domain.OrderItem orderItem = createDomain(org.minnal.examples.oms.domain.OrderItem.class); order.collection("orderItems").add(orderItem); order.persist(); ContainerResponse response = call(request( "/orders/" + order.getId() + "/order_items/", HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserializeCollection(getContent(response), java.util.List.class, org.minnal.examples.oms.domain.OrderItem.class) .size(), order.getOrderItems().size()); }
@Test public void readOrderOrderItemTest() { org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class); order.persist(); org.minnal.examples.oms.domain.OrderItem orderItem = createDomain(org.minnal.examples.oms.domain.OrderItem.class); order.collection("orderItems").add(orderItem); order.persist(); ContainerResponse response = call(request( "/orders/" + order.getId() + "/order_items/" + orderItem.getId(), HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserialize(getContent(response), org.minnal.examples.oms.domain.OrderItem.class) .getId(), orderItem.getId()); }
@Test public void updateOrderOrderItemTest() { org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class); order.persist(); org.minnal.examples.oms.domain.OrderItem orderItem = createDomain(org.minnal.examples.oms.domain.OrderItem.class); order.collection("orderItems").add(orderItem); order.persist(); org.minnal.examples.oms.domain.OrderItem modifiedorderItem = createDomain( org.minnal.examples.oms.domain.OrderItem.class, 1); ContainerResponse response = call(request( "/orders/" + order.getId() + "/order_items/" + orderItem.getId(), HttpMethod.PUT, serialize(modifiedorderItem))); assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); orderItem.merge(); assertTrue(compare(modifiedorderItem, orderItem, 1)); }
@Test public void deleteOrderOrderItemTest() { org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class); order.persist(); org.minnal.examples.oms.domain.OrderItem orderItem = createDomain(org.minnal.examples.oms.domain.OrderItem.class); order.collection("orderItems").add(orderItem); order.persist(); ContainerResponse response = call(request( "/orders/" + order.getId() + "/order_items/" + orderItem.getId(), HttpMethod.DELETE)); assertEquals(response.getStatus(), Response.Status.NO_CONTENT.getStatusCode()); response = call(request("/orders/" + order.getId() + "/order_items/" + orderItem.getId(), HttpMethod.GET, serialize(orderItem))); assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); }
/** * Creates the http response from container response</p> * * <li> Sets the content length to the http response from the container response. If content length is not available, computes from the response entity * <li> * * @param context * @param containerResponse * @param buffer * @return */ protected FullHttpResponse createHttpResponse(MessageContext context, ContainerResponse containerResponse, ByteBuf buffer) { FullHttpRequest httpRequest = context.getRequest(); DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(httpRequest.getProtocolVersion(), HttpResponseStatus.valueOf(containerResponse.getStatus()), buffer); int length = containerResponse.getLength(); // FIXME Hack. is there a better way? if (length == -1 && containerResponse.getEntity() instanceof String) { final String entity = (String) containerResponse.getEntity(); final byte[] encodedBytes = entity.getBytes(Charset.forName("UTF-8")); length = encodedBytes.length; } if (! containerResponse.getHeaders().containsKey(HttpHeaders.Names.CONTENT_LENGTH)) { HttpHeaders.setContentLength(httpResponse, length); logger.trace("Writing response status and headers {}, length {}", containerResponse, containerResponse.getLength()); } for (Map.Entry<String, List<Object>> headerEntry : containerResponse.getHeaders().entrySet()) { HttpHeaders.addHeader(httpResponse, headerEntry.getKey(), Joiner.on(", ").join(headerEntry.getValue())); } return httpResponse; }
private static RequestEvent event(String method, Integer status, Exception exception, String baseUri, String... uriTemplateStrings) { Builder builder = new RequestEventImpl.Builder(); ContainerRequest containerRequest = mock(ContainerRequest.class); when(containerRequest.getMethod()).thenReturn(method); builder.setContainerRequest(containerRequest); ContainerResponse containerResponse = mock(ContainerResponse.class); when(containerResponse.getStatus()).thenReturn(status); builder.setContainerResponse(containerResponse); builder.setException(exception, null); ExtendedUriInfo extendedUriInfo = mock(ExtendedUriInfo.class); when(extendedUriInfo.getBaseUri()).thenReturn( URI.create("http://localhost:8080" + (baseUri == null ? "/" : baseUri))); List<UriTemplate> uriTemplates = uriTemplateStrings == null ? Collections.emptyList() : Arrays.stream(uriTemplateStrings).map(uri -> new UriTemplate(uri)) .collect(Collectors.toList()); // UriTemplate are returned in reverse order Collections.reverse(uriTemplates); when(extendedUriInfo.getMatchedTemplates()).thenReturn(uriTemplates); builder.setExtendedUriInfo(extendedUriInfo); return builder.build(Type.FINISHED); }
@Test public void testJSONGeneratorConfigured() throws IOException { JSONPrettyPrintFilter filter = new JSONPrettyPrintFilter(); ContainerRequest request = ContainerRequestBuilder.from("http://localhost/foo/bar?pretty", "GET").accept("random/media").build(); ContainerResponse response = new ContainerResponse(request, Response.ok().build()); filter.filter(request, response); ObjectWriterModifier modifier = ObjectWriterInjector.get(); assertNotNull(modifier); JsonGenerator g = mock(JsonGenerator.class); modifier.modify(mock(JsonEndpointConfig.class), new MultivaluedHashMap<String, Object>(), new Object(), mock(ObjectWriter.class), g); verify(g).useDefaultPrettyPrinter(); }
@Test public void testJSONGeneratorUntouched() throws IOException { JSONPrettyPrintFilter filter = new JSONPrettyPrintFilter(); ContainerRequest request = ContainerRequestBuilder.from("http://localhost/foo/bar", "GET").accept("random/media").build(); ContainerResponse response = new ContainerResponse(request, Response.ok().build()); filter.filter(request, response); ObjectWriterModifier modifier = ObjectWriterInjector.get(); assertNull(modifier); }
/** * Writes the response status code and headers, returns an OutputStream that the Jersey application can write to. * @param contentLength The content length for the body * @param containerResponse The response object from the Jersey app * @return An OutputStream for Jersey to write the response body to * @throws ContainerException default Jersey declaration */ public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse containerResponse) throws ContainerException { statusCode = containerResponse.getStatusInfo().getStatusCode(); if (headers == null) { headers = new HashMap<>(); } for (final Map.Entry<String, List<String>> e : containerResponse.getStringHeaders().entrySet()) { for (final String value : e.getValue()) { // special case for set cookies // RFC 2109 allows for a comma separated list of cookies in one Set-Cookie header: https://tools.ietf.org/html/rfc2109 if (e.getKey().equals(HttpHeaders.SET_COOKIE)) { if (headers.containsKey(e.getKey()) && LambdaContainerHandler.getContainerConfig().isConsolidateSetCookieHeaders()) { headers.put(e.getKey(), headers.get(e.getKey()) + ", " + value); } else { headers.put(e.getKey(), containerResponse.getStringHeaders().getFirst(e.getKey())); break; } } else { headers.put(e.getKey(), value); } } } responseBody = new ByteArrayOutputStream(); return responseBody; }
@Test public void writeResponseStatusAndHeaders_ShouldReturnEntityOutputStreamOfResponse() { ContainerResponse context = mock(ContainerResponse.class); when(context.getStringHeaders()).thenReturn(new MultivaluedHashMap<>()); when(context.getStatusInfo()).thenReturn(Status.OK); OutputStream entityOutputStream = containerResponseWriter.writeResponseStatusAndHeaders(-1, context); assertSame(response.getEntityOutputStream(), entityOutputStream); }
@Test public void failure_ResponseNotYetCommitted_ShouldSetInternalServerErrorStatusOnFail() { ContainerResponse context = mock(ContainerResponse.class); when(context.getStatusInfo()).thenReturn(Status.OK); when(context.getStringHeaders()).thenReturn(new MultivaluedHashMap<>()); containerResponseWriter.writeResponseStatusAndHeaders(-1, context); containerResponseWriter.failure(new RuntimeException()); assertEquals(Status.INTERNAL_SERVER_ERROR, response.getStatusType()); }
/** * {@inheritDoc} */ @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { if (!responseContext.hasEntity() && !((ContainerResponse) responseContext).isMappedFromException() && responseContext.getStatus() >= 400 && responseContext.getStatus() < 600) { throw new WebApplicationException(responseContext.getStatus()); } List<MediaType> types = null; // 找不到 writer/reader, 提供系统有的 writer/reader 输出错误 if (responseContext.getStatus() == 406) { types = workersProvider.get() .getMessageBodyWriterMediaTypes( responseContext.getEntityClass(), responseContext.getEntityType(), responseContext.getEntityAnnotations()); } else if (responseContext.getStatus() == 415) { types = workersProvider.get() .getMessageBodyReaderMediaTypes( responseContext.getEntityClass(), responseContext.getEntityType(), responseContext.getEntityAnnotations()); } if (types != null) { ((ContainerResponse) responseContext).setMediaType(parseMediaType(types)); } }
@Override public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse jerseyResponse) throws ContainerException { int status = jerseyResponse.getStatus(); LOGGER.debug("{}: [{}] write status and headers st:{} cl:{}", Channels.getHexText(ctx), requestId, status, contentLength); meterStatus(status); // create the netty response HttpResponse nettyResponse = new DefaultHttpResponse(httpVersion, HttpResponseStatus.valueOf(status)); copyHeaders(jerseyResponse, nettyResponse); // add the request id to the header nettyResponse.headers().add(Headers.REQUEST_TRACING_HEADER, requestId); // add a Connection: Close header if required if (!keepAlive) { nettyResponse.headers().add(Names.CONNECTION, Values.CLOSE); } // create the content buffer if necessary if (contentLength < 0) { LOGGER.trace("{}: [{}] chunked", Channels.getHexText(ctx), requestId); nettyResponse.headers().add(Names.TRANSFER_ENCODING, Values.CHUNKED); ctx.writeAndFlush(nettyResponse); entityOutputStream = new EntityOutputStream(ctx, CONTENT_LENGTH_HISTOGRAM); } else if (contentLength == 0) { LOGGER.trace("{}: [{}] no content", Channels.getHexText(ctx), requestId); nettyResponse.headers().add(Names.CONTENT_LENGTH, 0); ctx.write(nettyResponse); entityOutputStream = new EmptyEntityOutputStream(ctx); } else { LOGGER.trace("{}: [{}] non-empty body", Channels.getHexText(ctx), requestId); nettyResponse.headers().add(Names.CONTENT_LENGTH, contentLength); ctx.write(nettyResponse); // don't flush now - only do so when all the content is written entityOutputStream = new EntityOutputStream(ctx, CONTENT_LENGTH_HISTOGRAM); } return entityOutputStream; }
private void copyHeaders(ContainerResponse sourceResponse, HttpResponse destinationResponse) { for (Map.Entry<String, List<Object>> header : sourceResponse.getHeaders().entrySet()) { List<String> values = Lists.newArrayList(); values.addAll(header.getValue().stream().map(Object::toString).collect(Collectors.toList())); destinationResponse.headers().add(header.getKey(), values); } }
/** * @see ContainerResponseWriter#writeResponseStatusAndHeaders(long, * ContainerResponse) */ @Override public OutputStream writeResponseStatusAndHeaders(final long contentLength, final ContainerResponse responseContext) { final List<Object> length = new ArrayList<Object>(); length.add(String.valueOf(contentLength)); responseContext.getHeaders().put(HttpHeaders.CONTENT_LENGTH, length); this.headers = responseContext.getStringHeaders(); this.statusInfo = responseContext.getStatusInfo(); return this.outputStream; }
/** * @see org.glassfish.jersey.server.spi.ContainerResponseWriter#writeResponseStatusAndHeaders(long, * org.glassfish.jersey.server.ContainerResponse) */ @Override public OutputStream writeResponseStatusAndHeaders(final long contentLength, final ContainerResponse responseContext) { final List<Object> length = Lists.newArrayList(); length.add(String.valueOf(contentLength)); responseContext.getHeaders().put(HttpHeaders.CONTENT_LENGTH, length); this.headers = responseContext.getStringHeaders(); this.statusInfo = responseContext.getStatusInfo(); return this.baos; }
@Override public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext) throws ContainerException { response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(responseContext.getStatus())); for (Map.Entry<String, List<Object>> header : responseContext.getHeaders().entrySet()) { } return new ByteBufOutputStream(response.content()); }
protected void requestCompleted(ContainerRequest request, ContainerResponse response) { if (contextCreated.get() == null) { return; } contextCreated.remove(); JPAContext context = getContext(); if (context.isTxnOpen()) { context.closeTxn(true); } context.close(); }
@BeforeMethod public void setup() { configuration = mock(DatabaseConfiguration.class); filter = spy(new OpenSessionInViewFilter(configuration)); context = mock(JPAContext.class); request = mock(ContainerRequest.class); response = mock(ContainerResponse.class); doReturn(context).when(filter).getContext(); }
@Test public void readOwnerTest() { org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class); owner.persist(); ContainerResponse response = call(request( "/owners/" + owner.getId(), HttpMethod.GET)); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); assertEquals(deserialize( getContent(response), org.minnal.examples.petclinic.domain.Owner.class) .getId(), owner.getId()); }
@Test public void createOwnerTest() { org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class); ContainerResponse response = call(request("/owners/", HttpMethod.POST, serialize(owner))); assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); }