Ejemplo n.º 1
0
  @Override
  public ClientResponse apply(final ClientRequest jerseyRequest) throws ProcessingException {
    final Request jettyRequest = translateRequest(jerseyRequest);
    final Map<String, String> clientHeadersSnapshot =
        writeOutBoundHeaders(jerseyRequest.getHeaders(), jettyRequest);
    final ContentProvider entity = getBytesProvider(jerseyRequest);
    if (entity != null) {
      jettyRequest.content(entity);
    }

    try {
      final ContentResponse jettyResponse = jettyRequest.send();
      HeaderUtils.checkHeaderChanges(
          clientHeadersSnapshot,
          jerseyRequest.getHeaders(),
          JettyConnector.this.getClass().getName());

      final javax.ws.rs.core.Response.StatusType status =
          jettyResponse.getReason() == null
              ? Statuses.from(jettyResponse.getStatus())
              : Statuses.from(jettyResponse.getStatus(), jettyResponse.getReason());

      final ClientResponse jerseyResponse = new ClientResponse(status, jerseyRequest);
      processResponseHeaders(jettyResponse.getHeaders(), jerseyResponse);
      try {
        jerseyResponse.setEntityStream(new HttpClientResponseInputStream(jettyResponse));
      } catch (final IOException e) {
        LOGGER.log(Level.SEVERE, null, e);
      }

      return jerseyResponse;
    } catch (final Exception e) {
      throw new ProcessingException(e);
    }
  }
Ejemplo n.º 2
0
  @Test
  public void testProxyWithoutContent() throws Exception {
    prepareProxy();
    prepareServer(
        new HttpServlet() {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
            if (req.getHeader("Via") != null) resp.addHeader(PROXIED_HEADER, "true");
          }
        });

    ContentResponse response =
        client
            .newRequest("localhost", serverConnector.getLocalPort())
            .timeout(5, TimeUnit.SECONDS)
            .send();

    Assert.assertEquals("OK", response.getReason());
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
  }