public long determineLength(HttpMessage httpMessage) throws HttpException {
   Args.notNull(httpMessage, "HTTP message");
   Header firstHeader = httpMessage.getFirstHeader(HTTP.TRANSFER_ENCODING);
   if (firstHeader != null) {
     String value = firstHeader.getValue();
     if (HTTP.CHUNK_CODING.equalsIgnoreCase(value)) {
       if (!httpMessage.getProtocolVersion().lessEquals(HttpVersion.HTTP_1_0)) {
         return -2;
       }
       throw new ProtocolException(
           "Chunked transfer encoding not allowed for " + httpMessage.getProtocolVersion());
     } else if (HTTP.IDENTITY_CODING.equalsIgnoreCase(value)) {
       return -1;
     } else {
       throw new ProtocolException("Unsupported transfer encoding: " + value);
     }
   }
   firstHeader = httpMessage.getFirstHeader(HTTP.CONTENT_LEN);
   if (firstHeader == null) {
     return (long) this.implicitLen;
   }
   String value2 = firstHeader.getValue();
   try {
     long parseLong = Long.parseLong(value2);
     if (parseLong >= 0) {
       return parseLong;
     }
     throw new ProtocolException("Negative content length: " + value2);
   } catch (NumberFormatException e) {
     throw new ProtocolException("Invalid content length: " + value2);
   }
 }
  /**
   * Sets the headers of an HTTP request necessary to execute.
   *
   * @param httpMessage The HTTP request to add the basic headers.
   * @param headers A map of key-value pairs representing the headers.
   */
  public static void setHeaders(HttpMessage httpMessage, Map<String, String> headers) {
    if (headers == null) {
      httpMessage.setHeader("Accept-Language", "en-us,en;q=0.5");
      return;
    } else if (!headers.containsKey("Accept-Language")) {
      headers.put("Accept-Language", "en-us,en;q=0.5");
    }

    for (Map.Entry<String, String> entry : headers.entrySet()) {
      httpMessage.setHeader(entry.getKey(), entry.getValue());
    }
  }
Ejemplo n.º 3
0
 public static UserAgent fromHttpMessage(HttpMessage httpMessage) {
   Header firstHeader = httpMessage.getFirstHeader(NAME);
   if (firstHeader == null) {
     return null;
   }
   return new UserAgent(firstHeader.getValue());
 }
Ejemplo n.º 4
0
 /*
  * Retrieves the full header value by combining multiple headers and
  * separating with commas, canonicalizing whitespace along the way.
  *
  * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
  */
 public static String getCanonicalHeaderValue(HttpMessage r, String name) {
   if (isSingleHeader(name)) {
     Header h = r.getFirstHeader(name);
     return (h != null) ? h.getValue() : null;
   }
   StringBuilder buf = new StringBuilder();
   boolean first = true;
   for (Header h : r.getHeaders(name)) {
     if (!first) {
       buf.append(", ");
     }
     buf.append(h.getValue().trim());
     first = false;
   }
   return buf.toString();
 }
Ejemplo n.º 5
0
 /*
  * Assert.asserts that all the headers appearing in r1 also appear in r2
  * with the same canonical header values.
  */
 public static boolean isEndToEndHeaderSubset(HttpMessage r1, HttpMessage r2) {
   for (Header h : r1.getAllHeaders()) {
     if (!isHopByHopHeader(h.getName())) {
       String r1val = getCanonicalHeaderValue(r1, h.getName());
       String r2val = getCanonicalHeaderValue(r2, h.getName());
       if (!r1val.equals(r2val)) return false;
     }
   }
   return true;
 }
  private Document getHttpMessageContent(HttpMessage message) {
    HttpEntity entity = null;

    if (message instanceof HttpEntityEnclosingRequest) {
      entity = ((HttpEntityEnclosingRequest) message).getEntity();
    } else if (message instanceof HttpResponse) {
      entity = ((HttpResponse) message).getEntity();
    }

    Document doc = null;

    // redirecting reply to and fault to nodes if present
    if (entity != null) {
      try (InputStream is = entity.getContent()) {
        ContentType contentType = null;
        for (Header header : message.getHeaders("Content-Type")) {
          try {
            contentType = ContentType.parse(header.getValue());
          } catch (ParseException | UnsupportedCharsetException ex) {
          }
        }

        if (contentType != null) {
          Charset charset = contentType.getCharset();
          if (charset == null) {
            charset = Charset.forName("UTF-8");
          }
          String mimeType = contentType.getMimeType();

          if (mimeType.contains("xml")) {
            String content = IOUtils.toString(is, charset.name());

            Document xmlDocument = XMLUtils.parseXML(content);
            if (xmlDocument != null) {
              XPath xpath = XPathFactory.newInstance().newXPath();
              xpath.setNamespaceContext(new FimsNamespaceContext());

              NodeList nodes =
                  (NodeList)
                      xpath.evaluate(
                          "/S:Envelope/S:Body/S:Fault/detail/*",
                          xmlDocument.getDocumentElement(),
                          XPathConstants.NODESET);

              if (nodes.getLength() == 0) {
                nodes =
                    (NodeList)
                        xpath.evaluate(
                            "/S:Envelope/S:Body/*",
                            xmlDocument.getDocumentElement(),
                            XPathConstants.NODESET);
              }

              doc = XMLUtils.newEmptyDocument();

              if (nodes.getLength() > 0) {
                Node node = nodes.item(0);
                Node newNode = doc.importNode(node, true);
                doc.appendChild(newNode);
              }
            }
          }
        }
      } catch (IOException | UnsupportedOperationException | XPathExpressionException ex) {
        Logger.getLogger(ValidationModuleImpl.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    return doc;
  }
Ejemplo n.º 7
0
 private void setMethodHeaders(HttpMessage method) {
   method.addHeader("Pragma", "no-cache");
   method.addHeader("Cache-Control", "no-cache");
 }