Java Code Examples for org.apache.commons.httpclient.HttpMethod#getRequestHeaders()

The following examples show how to use org.apache.commons.httpclient.HttpMethod#getRequestHeaders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUtilities.java    From odo with Apache License 2.0 6 votes vote down vote up
/**
 * Obtain newline-delimited headers from method
 *
 * @param method HttpMethod to scan
 * @return newline-delimited headers
 */
public static String getHeaders(HttpMethod method) {
    String headerString = "";
    Header[] headers = method.getRequestHeaders();
    for (Header header : headers) {
        String name = header.getName();
        if (name.equals(Constants.ODO_PROXY_HEADER)) {
            // skip.. don't want to log this
            continue;
        }

        if (headerString.length() != 0) {
            headerString += "\n";
        }

        headerString += header.getName() + ": " + header.getValue();
    }

    return headerString;
}
 
Example 2
Source File: BusinessJob.java    From http4e with Apache License 2.0 5 votes vote down vote up
private void addHeaders( HttpMethod httpMethod, Map<String, String> parameterizedMap){
   for (Iterator it = model.getHeaders().entrySet().iterator(); it.hasNext();) {
      Map.Entry me = (Map.Entry) it.next();
      String key = (String) me.getKey();
      List values = (List) me.getValue();
      StringBuilder sb = new StringBuilder();
      int cnt = 0;
      for (Iterator it2 = values.iterator(); it2.hasNext();) {
         String val = (String) it2.next();
         if (cnt != 0) {
            sb.append(",");
         }
         sb.append(val);
         cnt++;
      }

      String parameterizedVal = ParseUtils.getParametizedArg(sb.toString(), parameterizedMap);
      httpMethod.addRequestHeader(key, parameterizedVal);
   }

   // add User-Agent: ProjectName
   boolean userAgentExist = false;
   Header[] hhh = httpMethod.getRequestHeaders();
   for (int i = 0; i < hhh.length; i++) {
      Header h = hhh[i];
      if (CoreConstants.HEADER_USER_AGENT.equalsIgnoreCase(h.getName())) {
         userAgentExist = true;
      }
   }

   if (!userAgentExist) {
      httpMethod.addRequestHeader(CoreConstants.HEADER_USER_AGENT, CoreMessages.PLUGIN_NAME_SHORT + "/" + CoreMessages.VERSION);
   }
}
 
Example 3
Source File: HeaderProcessorTest.java    From elasticsearch-hadoop with Apache License 2.0 5 votes vote down vote up
private static Header[] applyHeaders(Settings settings) {
    HeaderProcessor processor = new HeaderProcessor(settings);

    HttpMethod method = new PostMethod("http://localhost:9200");
    processor.applyTo(method);

    return method.getRequestHeaders();
}
 
Example 4
Source File: Http3SignatureAuthScheme.java    From httpsig-java with The Unlicense 4 votes vote down vote up
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
    if (credentials instanceof SignerCredentials) {
        SignerCredentials creds = (SignerCredentials) credentials;
        String headers = this.getParameter(Constants.HEADERS);
        String algorithms = this.getParameter(Constants.ALGORITHMS);

        Challenge challenge = new Challenge(this.getRealm(), Constants.parseTokens(headers), Challenge.parseAlgorithms(algorithms));

        Signer signer = creds.getSigner();
        if (signer != null) {

            if (this.rotate) {
                this.rotate = false;
                if (!signer.rotateKeys(challenge, this.lastAuthz)) {
                    signer.rotateKeys(challenge);
                    return null;
                }
            }

            RequestContent.Builder sigBuilder = new RequestContent.Builder();

            sigBuilder.setRequestTarget(method.getName(), method.getPath() + (method.getQueryString() != null ? "?" + method.getQueryString() : ""));

            for (Header header : method.getRequestHeaders()) {
                sigBuilder.addHeader(header.getName(), header.getValue());
            }

            if (sigBuilder.build().getDate() == null) {
                sigBuilder.addDateNow();
                method.addRequestHeader(Constants.HEADER_DATE, sigBuilder.build().getDate());
            }

            Authorization authorization = creds.getSigner().sign(sigBuilder.build());
            this.lastAuthz = authorization;
            if (authorization != null) {
                return authorization.getHeaderValue();
            }
        }
    }

    return null;
}