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

The following examples show how to use org.apache.commons.httpclient.HttpMethod#getResponseHeaders() . 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: HttpUtils.java    From http4e with Apache License 2.0 6 votes vote down vote up
public static void dumpResponse( HttpMethod httpmethod, PrintStream out){

      try {
         out.println("------------");
         out.println(httpmethod.getStatusLine().toString());

         Header[] h = httpmethod.getResponseHeaders();
         for (int i = 0; i < h.length; i++) {
            out.println(h[i].getName() + ": " + h[i].getValue());
         }

         InputStreamReader inR = new InputStreamReader(httpmethod.getResponseBodyAsStream());
         BufferedReader buf = new BufferedReader(inR);
         String line;
         while ((line = buf.readLine()) != null) {
            out.println(line);
         }
         out.println("------------");

      } catch (Exception e) {
         throw new RuntimeException(e);
      }
   }
 
Example 2
Source File: HttpClientConnection.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public String getHeaderField(int nth)
// throws IOException TODO: doesn't this one throw exceptions??
{
  try {
    HttpMethod res = getResult(true);
    Header[] hs = res.getResponseHeaders();

    if (hs.length > nth && nth >= 0) {
      return hs[nth].getValue();
    } else {
      return null;
    }
  } catch (IOException e) {
    return null;
  }
}
 
Example 3
Source File: DefaultDiamondSubscriber.java    From diamond with Apache License 2.0 5 votes vote down vote up
/**
 * 设置新的消息轮询间隔时间
 * 
 * @param httpMethod
 */
void changeSpacingInterval(HttpMethod httpMethod) {
    Header[] spacingIntervalHeaders = httpMethod.getResponseHeaders(Constants.SPACING_INTERVAL);
    if (spacingIntervalHeaders.length >= 1) {
        try {
            diamondConfigure.setPollingIntervalTime(Integer.parseInt(spacingIntervalHeaders[0].getValue()));
        }
        catch (RuntimeException e) {
            log.error("设置下次间隔时间失败", e);
        }
    }
}
 
Example 4
Source File: DefaultDiamondSubscriber.java    From diamond with Apache License 2.0 5 votes vote down vote up
/**
 * 设置新的消息轮询间隔时间
 * 
 * @param httpMethod
 */
void changeSpacingInterval(HttpMethod httpMethod) {
    Header[] spacingIntervalHeaders = httpMethod.getResponseHeaders(Constants.SPACING_INTERVAL);
    if (spacingIntervalHeaders.length >= 1) {
        try {
            diamondConfigure.setPollingIntervalTime(Integer.parseInt(spacingIntervalHeaders[0].getValue()));
        }
        catch (RuntimeException e) {
            log.error("设置下次间隔时间失败", e);
        }
    }
}
 
Example 5
Source File: HttpUtils.java    From http4e with Apache License 2.0 5 votes vote down vote up
public static void dumpResponse( HttpMethod httpmethod, StringBuilder httpResponsePacket, StringBuilder respBody){
   try {
      httpResponsePacket.append('\n');
      httpResponsePacket.append(httpmethod.getStatusLine().toString());
      httpResponsePacket.append('\n');

      Header[] h = httpmethod.getResponseHeaders();
      for (int i = 0; i < h.length; i++) {
         httpResponsePacket.append(h[i].getName() + ": " + h[i].getValue());
         httpResponsePacket.append('\n');
      }

      InputStreamReader inR = new InputStreamReader(httpmethod.getResponseBodyAsStream());
      BufferedReader buf = new BufferedReader(inR);
      String line;
      while ((line = buf.readLine()) != null) {
         httpResponsePacket.append(line);
         httpResponsePacket.append('\n');
         respBody.append(line);
         respBody.append('\n');
      }
      httpResponsePacket.append('\n');

   } catch (Exception e) {
      throw new RuntimeException(e);
   }
}
 
Example 6
Source File: HttpClientConnection.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String getHeaderFieldKey(int nth) throws IOException {
  HttpMethod res = getResult(true);
  Header[] hs = res.getResponseHeaders();

  if (hs.length > nth && nth >= 0) {
    return hs[nth].getName();
  } else {
    return null;
  }
}
 
Example 7
Source File: TraceeHttpClientDecorator.java    From tracee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void postResponse(HttpMethod httpMethod) {
	if (!httpMethod.isRequestSent()) return;
	final Header[] responseHeaders = httpMethod.getResponseHeaders(TraceeConstants.TPIC_HEADER);
	final TraceeFilterConfiguration filterConfiguration = backend.getConfiguration(profile);
	if (responseHeaders != null && responseHeaders.length > 0 && filterConfiguration.shouldProcessContext(IncomingResponse)) {
		final List<String> stringTraceeHeaders = new ArrayList<>();
		for (Header header : responseHeaders) {
			stringTraceeHeaders.add(header.getValue());
		}

		backend.putAll(filterConfiguration.filterDeniedParams(transportSerialization.parse(stringTraceeHeaders), IncomingResponse));
	}
}
 
Example 8
Source File: Proxy.java    From odo with Apache License 2.0 4 votes vote down vote up
/**
 * Execute a request
 *
 * @param httpMethodProxyRequest
 * @param httpServletRequest
 * @param httpServletResponse
 * @param history
 * @throws Exception
 */
private void executeRequest(HttpMethod httpMethodProxyRequest,
                            HttpServletRequest httpServletRequest,
                            PluginResponse httpServletResponse,
                            History history) throws Exception {
    int intProxyResponseCode = 999;
    // Create a default HttpClient
    HttpClient httpClient = new HttpClient();
    HttpState state = new HttpState();

    try {
        httpMethodProxyRequest.setFollowRedirects(false);
        ArrayList<String> headersToRemove = getRemoveHeaders();

        httpClient.getParams().setSoTimeout(60000);

        httpServletRequest.setAttribute("com.groupon.odo.removeHeaders", headersToRemove);

        // exception handling for httpclient
        HttpMethodRetryHandler noretryhandler = new HttpMethodRetryHandler() {
            public boolean retryMethod(
                final HttpMethod method,
                final IOException exception,
                int executionCount) {
                return false;
            }
        };

        httpMethodProxyRequest.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, noretryhandler);

        intProxyResponseCode = httpClient.executeMethod(httpMethodProxyRequest.getHostConfiguration(), httpMethodProxyRequest, state);
    } catch (Exception e) {
        // Return a gateway timeout
        httpServletResponse.setStatus(504);
        httpServletResponse.setHeader(Constants.HEADER_STATUS, "504");
        httpServletResponse.flushBuffer();
        return;
    }
    logger.info("Response code: {}, {}", intProxyResponseCode,
                HttpUtilities.getURL(httpMethodProxyRequest.getURI().toString()));

    // Pass the response code back to the client
    httpServletResponse.setStatus(intProxyResponseCode);

    // Pass response headers back to the client
    Header[] headerArrayResponse = httpMethodProxyRequest.getResponseHeaders();
    for (Header header : headerArrayResponse) {
        // remove transfer-encoding header.  The http libraries will handle this encoding
        if (header.getName().toLowerCase().equals("transfer-encoding")) {
            continue;
        }

        httpServletResponse.setHeader(header.getName(), header.getValue());
    }

    // there is no data for a HTTP 304 or 204
    if (intProxyResponseCode != HttpServletResponse.SC_NOT_MODIFIED &&
        intProxyResponseCode != HttpServletResponse.SC_NO_CONTENT) {
        // Send the content to the client
        httpServletResponse.resetBuffer();
        httpServletResponse.getOutputStream().write(httpMethodProxyRequest.getResponseBody());
    }

    // copy cookies to servlet response
    for (Cookie cookie : state.getCookies()) {
        javax.servlet.http.Cookie servletCookie = new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue());

        if (cookie.getPath() != null) {
            servletCookie.setPath(cookie.getPath());
        }

        if (cookie.getDomain() != null) {
            servletCookie.setDomain(cookie.getDomain());
        }

        // convert expiry date to max age
        if (cookie.getExpiryDate() != null) {
            servletCookie.setMaxAge((int) ((cookie.getExpiryDate().getTime() - System.currentTimeMillis()) / 1000));
        }

        servletCookie.setSecure(cookie.getSecure());

        servletCookie.setVersion(cookie.getVersion());

        if (cookie.getComment() != null) {
            servletCookie.setComment(cookie.getComment());
        }

        httpServletResponse.addCookie(servletCookie);
    }
}