@Test
  public void testApplicationResource() throws Exception {
    final Response response = target().request().get();
    assertThat(response.readEntity(String.class), is("writer-resource"));
    assertThat(response.getStringHeaders().getFirst("foo"), is("bar"));

    checkDestroyed(false);
    reloader.reload(new ResourceConfig(Resource.class));
    checkDestroyed(true);
  }
Example #2
0
 @Test
 public void getStringHeadersUsingHeaderDelegateTest() {
   RuntimeDelegate original = RuntimeDelegate.getInstance();
   RuntimeDelegate.setInstance(new StringBeanRuntimeDelegate(original));
   try {
     StringBuilder builder = new StringBuilder("s1");
     StringBuffer buffer = new StringBuffer("s2");
     StringBean bean = new StringBean("s3");
     Response response =
         Response.ok()
             .header(builder.toString(), builder)
             .header(buffer.toString(), buffer)
             .header(bean.get(), bean)
             .build();
     MultivaluedMap<String, String> headers = response.getStringHeaders();
     String header = headers.getFirst(bean.get());
     Assert.assertTrue(bean.get().equalsIgnoreCase(header));
   } finally {
     RuntimeDelegate.setInstance(original);
     StringBeanRuntimeDelegate.assertNotStringBeanRuntimeDelegate();
   }
 }
Example #3
0
  public static String dumpResponse(Response r, boolean detailed, String fieldDelim) {
    String s = "[Response obiect] ";
    s += "status=" + r.getStatus();
    s += fieldDelim + "statusInfo=" + r.getStatusInfo();
    s += fieldDelim + "location=" + r.getLocation();

    if (!detailed) return s;

    MultivaluedMap<String, String> headersStrings = r.getStringHeaders();
    Iterator<String> it = headersStrings.keySet().iterator();
    s += "\n---- headers ---";
    while (it.hasNext()) {
      String theKey = it.next();
      s += "\n" + theKey + ":";
      List<String> valuesList = headersStrings.get(theKey);
      for (String value : valuesList) {
        s += value + ",";
      }
    }

    return s;
  }
Example #4
0
  protected ResponseBuilder setResponseBuilder(Message outMessage, Exchange exchange)
      throws Exception {
    Response response = exchange.get(Response.class);
    if (response != null) {
      outMessage
          .getExchange()
          .getInMessage()
          .put(Message.PROTOCOL_HEADERS, response.getStringHeaders());
      return JAXRSUtils.fromResponse(JAXRSUtils.copyResponseIfNeeded(response));
    }

    Integer status = getResponseCode(exchange);
    ResponseBuilder currentResponseBuilder = JAXRSUtils.toResponseBuilder(status);

    Message responseMessage =
        exchange.getInMessage() != null ? exchange.getInMessage() : exchange.getInFaultMessage();
    // if there is no response message, we just send the response back directly
    if (responseMessage == null) {
      return currentResponseBuilder;
    }

    Map<String, List<Object>> protocolHeaders =
        CastUtils.cast((Map<?, ?>) responseMessage.get(Message.PROTOCOL_HEADERS));

    boolean splitHeaders =
        MessageUtils.isTrue(outMessage.getContextualProperty(HEADER_SPLIT_PROPERTY));
    for (Map.Entry<String, List<Object>> entry : protocolHeaders.entrySet()) {
      if (null == entry.getKey()) {
        continue;
      }
      if (entry.getValue().size() > 0) {
        if (HttpUtils.isDateRelatedHeader(entry.getKey())) {
          currentResponseBuilder.header(entry.getKey(), entry.getValue().get(0));
          continue;
        }
        for (Object valObject : entry.getValue()) {
          if (splitHeaders && valObject instanceof String) {
            String val = (String) valObject;
            String[] values;
            if (val == null || val.length() == 0) {
              values = new String[] {""};
            } else if (val.charAt(0) == '"' && val.charAt(val.length() - 1) == '"') {
              // if the value starts with a quote and ends with a quote, we do a best
              // effort attempt to determine what the individual values are.
              values = parseQuotedHeaderValue(val);
            } else {
              boolean splitPossible =
                  !(HttpHeaders.SET_COOKIE.equalsIgnoreCase(entry.getKey())
                      && val.toUpperCase().contains(HttpHeaders.EXPIRES.toUpperCase()));
              values = splitPossible ? val.split(",") : new String[] {val};
            }
            for (String s : values) {
              String theValue = s.trim();
              if (theValue.length() > 0) {
                currentResponseBuilder.header(entry.getKey(), theValue);
              }
            }
          } else {
            currentResponseBuilder.header(entry.getKey(), valObject);
          }
        }
      }
    }
    String ct = (String) responseMessage.get(Message.CONTENT_TYPE);
    if (ct != null) {
      currentResponseBuilder.type(ct);
    }
    InputStream mStream = responseMessage.getContent(InputStream.class);
    currentResponseBuilder.entity(mStream);

    return currentResponseBuilder;
  }