List of usage examples for org.apache.commons.httpclient HttpMethod getStatusCode
public abstract int getStatusCode();
From source file:it.infn.ct.aleph_portlet.java
public void getRecordsOAR(String search, int jrec, int num_rec) { String responseXML = null;/*from w w w .j a va 2 s . c om*/ HttpClient client = new HttpClient(); HttpMethod method = callAPIOAR(search, jrec, num_rec); try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { method.getResponseBody(); responseXML = convertStreamToString(method.getResponseBodyAsStream()); FileWriter fw = new FileWriter( appServerPath + "datatable/marcXML_OAR_" + jrec + "_" + num_rec + ".xml"); System.out.println(); fw.append(responseXML); fw.close(); } } catch (IOException e) { e.printStackTrace(); } finally { method.releaseConnection(); } }
From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java
/** * This method populates <code>curi</code> with response status and * content type.//from w w w . j a v a 2 s . c o m * @param curi CrawlURI to populate. * @param method Method to get response status and headers from. */ protected void addResponseContent(HttpMethod method, CrawlURI curi) { curi.setFetchStatus(method.getStatusCode()); Header ct = method.getResponseHeader("content-type"); curi.setContentType((ct == null) ? null : ct.getValue()); // Save method into curi too. Midfetch filters may want to leverage // info in here. curi.putObject(A_HTTP_TRANSACTION, method); }
From source file:io.hops.hopsworks.api.admin.HDFSUIProxyServlet.java
@Override protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException { if (servletRequest.getUserPrincipal() == null) { servletResponse.sendError(403, "User is not logged in"); return;//ww w .j a va2s .co m } if (!servletRequest.isUserInRole("HOPS_ADMIN")) { servletResponse.sendError(Response.Status.BAD_REQUEST.getStatusCode(), "You don't have the access right for this service"); return; } if (servletRequest.getAttribute(ATTR_TARGET_URI) == null) { servletRequest.setAttribute(ATTR_TARGET_URI, targetUri); } if (servletRequest.getAttribute(ATTR_TARGET_HOST) == null) { servletRequest.setAttribute(ATTR_TARGET_HOST, targetHost); } // Make the Request // note: we won't transfer the protocol version because I'm not // sure it would truly be compatible String proxyRequestUri = rewriteUrlFromRequest(servletRequest); try { String[] targetHost_port = settings.getHDFSWebUIAddress().split(":"); File keyStore = new File(baseHadoopClientsService.getSuperKeystorePath()); File trustStore = new File(baseHadoopClientsService.getSuperTrustStorePath()); // Assume that KeyStore password and Key password are the same Protocol httpsProto = new Protocol("https", new CustomSSLProtocolSocketFactory(keyStore, baseHadoopClientsService.getSuperKeystorePassword(), baseHadoopClientsService.getSuperKeystorePassword(), trustStore, baseHadoopClientsService.getSuperTrustStorePassword()), Integer.parseInt(targetHost_port[1])); Protocol.registerProtocol("https", httpsProto); // Execute the request HttpClientParams params = new HttpClientParams(); params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); HttpClient client = new HttpClient(params); HostConfiguration config = new HostConfiguration(); InetAddress localAddress = InetAddress.getLocalHost(); config.setLocalAddress(localAddress); HttpMethod m = new GetMethod(proxyRequestUri); Enumeration<String> names = servletRequest.getHeaderNames(); while (names.hasMoreElements()) { String headerName = names.nextElement(); String value = servletRequest.getHeader(headerName); if (PASS_THROUGH_HEADERS.contains(headerName)) { //hdfs does not send back the js if encoding is not accepted //but we don't want to accept encoding for the html because we //need to be able to parse it if (headerName.equalsIgnoreCase("accept-encoding") && (servletRequest.getPathInfo() == null || !servletRequest.getPathInfo().contains(".js"))) { continue; } else { m.setRequestHeader(headerName, value); } } } String user = servletRequest.getRemoteUser(); if (user != null && !user.isEmpty()) { m.setRequestHeader("Cookie", "proxy-user" + "=" + URLEncoder.encode(user, "ASCII")); } client.executeMethod(config, m); // Process the response int statusCode = m.getStatusCode(); // Pass the response code. This method with the "reason phrase" is //deprecated but it's the only way to pass the reason along too. //noinspection deprecation servletResponse.setStatus(statusCode, m.getStatusLine().getReasonPhrase()); copyResponseHeaders(m, servletRequest, servletResponse); // Send the content to the client copyResponseEntity(m, servletResponse); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } if (e instanceof ServletException) { throw (ServletException) e; } //noinspection ConstantConditions if (e instanceof IOException) { throw (IOException) e; } throw new RuntimeException(e); } }
From source file:gov.loc.ndmso.proxyfilter.RequestProxy.java
private static void setupResponseHeaders(HttpMethod httpMethod, HttpServletResponse hsResponse) { //if ( log.isInfoEnabled() ) { // log.info("setupResponseHeaders"); // log.info("status text: " + httpMethod.getStatusText()); // log.info("status line: " + httpMethod.getStatusLine()); //}//from w ww .j a v a2s . co m //String ctHeaderFullStr = ""; String ctHeaderStr = ""; boolean ctHeaderExists = false; for (int i = 0; i < httpMethod.getResponseHeaders().length; i++) { Header h = httpMethod.getResponseHeaders()[i]; if ("content-type".equalsIgnoreCase(h.getName())) { //ctHeaderFullStr = h.toString(); ctHeaderStr = h.getValue(); //log.info("ctHeaderFullStr is " + ctHeaderFullStr); //log.info("ctHeaderStr is " + ctHeaderStr); if (ctHeaderStr.indexOf("audio/mpeg") > -1) { //log.info("We have a match. ctHeaderExists is true"); ctHeaderExists = true; } } } //filter the headers, which are copied from the proxy response. The http lib handles those itself. //Filtered out: the content encoding, the content length and cookies for (int i = 0; i < httpMethod.getResponseHeaders().length; i++) { Header h = httpMethod.getResponseHeaders()[i]; if ("content-encoding".equalsIgnoreCase(h.getName())) { continue; } else if ("content-length".equalsIgnoreCase(h.getName()) && !ctHeaderExists) { continue; } else if ("transfer-encoding".equalsIgnoreCase(h.getName())) { continue; } else if (h.getName().toLowerCase().startsWith("cookie")) { //retrieving a cookie which sets the session id will change the calling session: bad! So we skip this header. continue; } else if (h.getName().toLowerCase().startsWith("set-cookie")) { //retrieving a cookie which sets the session id will change the calling session: bad! So we skip this header. continue; } hsResponse.addHeader(h.getName(), h.getValue()); // log.info("setting response parameter:" + h.getName() + ", value: " + h.getValue()); } // fix me what about the response footers? (httpMethod.getResponseFooters()) if (httpMethod.getStatusCode() != 200) { hsResponse.setStatus(httpMethod.getStatusCode()); } }
From source file:com.intuit.tank.httpclient3.TankHttpClient3.java
private void sendRequest(BaseRequest request, @Nonnull HttpMethod method, String requestBody) { String uri = null;//w w w. ja v a2 s . co m long waitTime = 0L; try { uri = method.getURI().toString(); logger.debug(request.getLogUtil().getLogMessage( "About to " + method.getName() + " request to " + uri + " with requestBody " + requestBody, LogEventType.Informational)); List<String> cookies = new ArrayList<String>(); if (httpclient != null && httpclient.getState() != null && httpclient.getState().getCookies() != null) { for (Cookie cookie : httpclient.getState().getCookies()) { cookies.add("REQUEST COOKIE: " + cookie.toExternalForm() + " (domain=" + cookie.getDomain() + " : path=" + cookie.getPath() + ")"); } } request.logRequest(uri, requestBody, method.getName(), request.getHeaderInformation(), cookies, false); setHeaders(request, method, request.getHeaderInformation()); long startTime = System.currentTimeMillis(); request.setTimestamp(new Date(startTime)); httpclient.executeMethod(method); // read response body byte[] responseBody = new byte[0]; // check for no content headers if (method.getStatusCode() != 203 && method.getStatusCode() != 202 && method.getStatusCode() != 204) { try { InputStream httpInputStream = method.getResponseBodyAsStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); int curByte = httpInputStream.read(); while (curByte >= 0) { out.write(curByte); curByte = httpInputStream.read(); } responseBody = out.toByteArray(); } catch (Exception e) { logger.warn("could not get response body: " + e); } } long endTime = System.currentTimeMillis(); processResponse(responseBody, startTime, endTime, request, method.getStatusText(), method.getStatusCode(), method.getResponseHeaders(), httpclient.getState()); waitTime = endTime - startTime; } catch (Exception ex) { logger.error(request.getLogUtil().getLogMessage( "Could not do " + method.getName() + " to url " + uri + " | error: " + ex.toString(), LogEventType.IO), ex); throw new RuntimeException(ex); } finally { try { method.releaseConnection(); } catch (Exception e) { logger.warn("Could not release connection: " + e, e); } if (method.getName().equalsIgnoreCase("post") && request.getLogUtil().getAgentConfig().getLogPostResponse()) { logger.info(request.getLogUtil() .getLogMessage("Response from POST to " + request.getRequestUrl() + " got status code " + request.getResponse().getHttpCode() + " BODY { " + request.getResponse().getBody() + " }", LogEventType.Informational)); } } if (waitTime != 0) { doWaitDueToLongResponse(request, waitTime, uri); } }
From source file:ar.com.zauber.commons.web.proxy.HttpClientRequestProxy.java
/** * @see AbstractController#handleRequestInternal(HttpServletRequest, * HttpServletResponse)/*from w w w.j a v a 2 s.c om*/ * @throws Exception . */ public final void handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response) throws Exception { final URLResult r = urlRequestMapper.getProxiedURLFromRequest(request); if (r.hasResult()) { final HttpMethod method = buildRequest(request, r); InputStream is = null; try { // Entity enclosing requests cannot be redirected // without user intervention according to RFC 2616 if (!method.getName().equalsIgnoreCase("post") && !method.getName().equalsIgnoreCase("put")) { method.setFollowRedirects(followRedirects); } httpClient.executeMethod(method); updateResponseCode(request, response, method); proxyHeaders(response, method); addOtherHeaders(response, method); is = method.getResponseBodyAsStream(); if (is != null) { if (contentTransformer.getContentType() != null) { response.setContentType(contentTransformer.getContentType()); } try { String uri = request.getPathInfo(); // When using this component as a Filter, the path info // can be null, but the same information is available at // getServletPath if (uri == null) { uri = request.getServletPath(); } contentTransformer.transform(is, response.getOutputStream(), new InmutableContentMetadata(uri, getContentType(method), method.getStatusCode())); } finally { is.close(); } } } catch (final ConnectException e) { onConnectionException(request, response, method, e); } finally { if (is != null) { is.close(); } method.releaseConnection(); } } else { onNoMapping(request, response); } }
From source file:com.cordys.coe.ac.httpconnector.samples.JIRAResponseHandler.java
/** * This method checks the HTML for errors during processing. * /*w w w .j a v a 2 s.c o m*/ * @param document * The HTML document. * @param httpMethod * The actual HTTP method that was executed. * * @throws HandlerException * In case the response contains any functional errors. * @throws XPathExpressionException * In case one of the XPaths fail. */ protected void checkErrors(org.w3c.dom.Document document, HttpMethod httpMethod) throws HandlerException, XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xpath.evaluate("//span[@class='errMsg']/text()", document, XPathConstants.NODESET); int length = nodeList.getLength(); if (length != 0) { // The first error message found will be used. HandlerException he = new HandlerException( HandlerExceptionMessages.ERROR_CONVERTING_THE_RESPONSE_TO_PROPER_XML, nodeList.item(0).getNodeValue()); for (int i = 0; i < length; i++) { Node node = nodeList.item(i); he.addAdditionalErrorMessage(node.getNodeValue()); } throw he; } // There is another possibility of which errors might be returned. There // is a td with // class formErrors. And it that holds the errors as list items. nodeList = (NodeList) xpath.evaluate("//td[@class='formErrors']/div[@class='errorArea']/ul/li/text()", document, XPathConstants.NODESET); length = nodeList.getLength(); if (length != 0) { // The first error message found will be used. HandlerException he = new HandlerException( HandlerExceptionMessages.ERROR_CONVERTING_THE_RESPONSE_TO_PROPER_XML, nodeList.item(0).getNodeValue()); for (int i = 0; i < length; i++) { Node node = nodeList.item(i); he.addAdditionalErrorMessage(node.getNodeValue()); } throw he; } if (httpMethod.getStatusCode() == 500) { // Find the short description Node n = (Node) xpath.evaluate("//b[.='Cause: ']", document, XPathConstants.NODE); String shortError = n.getNextSibling().getNextSibling().getNodeValue().trim(); // The first error message found will be used. HandlerException he = new HandlerException( HandlerExceptionMessages.ERROR_CONVERTING_THE_RESPONSE_TO_PROPER_XML, "System Error: " + shortError); // Find the stacktrace if available. he.addAdditionalErrorMessage( (String) xpath.evaluate("//pre[@id='stacktrace']/text()", document, XPathConstants.STRING)); throw he; } }
From source file:ch.ksfx.web.services.spidering.http.WebEngine.java
public void loadResource(Resource resource) { HttpMethod httpMethod; try {// w ww. jav a 2s . c o m if (/*resource.getHttpMethod().equals(GET)*/true) { String url = resource.getUrl(); if (url != null) { url = url.replaceAll("&", "&"); url = url.replaceAll(""", "\""); } httpMethod = this.httpClientHelper.executeGetMethod(url); } else { //TODO implement POST functionality /* NameValuePair[] nameValuePairs = new NameValuePair[resource.getPostData().size()]; for(int i = 0; i < resource.getPostData().size(); i++) { nameValuePairs[i] = new NameValuePair(resource.getPostData().get(i).getName(), resource.getPostData().get(i).getValue()); } String url = resource.getURL().toString(); if (url != null) { url = url.replaceAll("&","&"); url = url.replaceAll(""","\""); } httpMethod = this.httpClientHelper.executePostMethod(url, nameValuePairs); */ } } catch (Exception e) { resource.setLoadSucceed(false); resource.setHttpStatusCode(222); logger.log(Level.SEVERE, "Unable to load resource", e); return; } if (httpMethod == null) { resource.setLoadSucceed(false); return; } if (httpMethod.getStatusCode() != HttpStatus.SC_OK) { try { resource.setUrl(httpMethod.getURI().toString()); for (Header header : httpMethod.getResponseHeaders()) { resource.addResponseHeader(new ResponseHeader(header.getName(), header.getValue())); } resource.setHttpStatusCode(httpMethod.getStatusCode()); } catch (Exception e) { logger.warning(e.getMessage()); e.printStackTrace(); } return; } try { if (httpMethod.getResponseHeader("Content-Encoding") != null && httpMethod.getResponseHeader("Content-Encoding").getValue().contains("gzip")) { BufferedInputStream in = new BufferedInputStream( new GZIPInputStream(httpMethod.getResponseBodyAsStream())); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) >= 0) { out.write(buffer, 0, length); } resource.setRawContent(out.toByteArray()); resource.setSize(new Long(out.toByteArray().length)); } else { BufferedInputStream in = new BufferedInputStream(httpMethod.getResponseBodyAsStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) >= 0) { out.write(buffer, 0, length); } resource.setRawContent(out.toByteArray()); resource.setSize(new Long(out.toByteArray().length)); } resource.setHttpStatusCode(httpMethod.getStatusCode()); resource.setLoadSucceed(true); resource.setMimeType(recognizeMimeType(resource, httpMethod)); if (resource.getMimeType().startsWith("text") || resource.getMimeType().equals("application/json")) { resource.setContent(EncodingHelper.encode(resource.getRawContent(), resource.getEncoding(), ((HttpMethodBase) httpMethod).getResponseCharSet())); } else { resource.setIsBinary(true); } } catch (IOException e) { e.printStackTrace(); logger.log(Level.SEVERE, "Unable to load resource", e); } finally { httpMethod.releaseConnection(); } }
From source file:io.hops.hopsworks.api.admin.YarnUIProxyServlet.java
@Override protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException { if (servletRequest.getUserPrincipal() == null) { servletResponse.sendError(403, "User is not logged in"); return;/* www. j av a 2s . co m*/ } if (!servletRequest.isUserInRole("HOPS_ADMIN")) { servletResponse.sendError(Response.Status.BAD_REQUEST.getStatusCode(), "You don't have the access right for this service"); return; } if (servletRequest.getAttribute(ATTR_TARGET_URI) == null) { servletRequest.setAttribute(ATTR_TARGET_URI, targetUri); } if (servletRequest.getAttribute(ATTR_TARGET_HOST) == null) { servletRequest.setAttribute(ATTR_TARGET_HOST, targetHost); } // Make the Request // note: we won't transfer the protocol version because I'm not // sure it would truly be compatible String proxyRequestUri = rewriteUrlFromRequest(servletRequest); try { // Execute the request HttpClientParams params = new HttpClientParams(); params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); HttpClient client = new HttpClient(params); HostConfiguration config = new HostConfiguration(); InetAddress localAddress = InetAddress.getLocalHost(); config.setLocalAddress(localAddress); String method = servletRequest.getMethod(); HttpMethod m; if (method.equalsIgnoreCase("PUT")) { m = new PutMethod(proxyRequestUri); RequestEntity requestEntity = new InputStreamRequestEntity(servletRequest.getInputStream(), servletRequest.getContentType()); ((PutMethod) m).setRequestEntity(requestEntity); } else { m = new GetMethod(proxyRequestUri); } Enumeration<String> names = servletRequest.getHeaderNames(); while (names.hasMoreElements()) { String headerName = names.nextElement(); String value = servletRequest.getHeader(headerName); if (PASS_THROUGH_HEADERS.contains(headerName)) { //yarn does not send back the js if encoding is not accepted //but we don't want to accept encoding for the html because we //need to be able to parse it if (headerName.equalsIgnoreCase("accept-encoding") && (servletRequest.getPathInfo() == null || !servletRequest.getPathInfo().contains(".js"))) { continue; } else { m.setRequestHeader(headerName, value); } } } String user = servletRequest.getRemoteUser(); if (user != null && !user.isEmpty()) { m.setRequestHeader("Cookie", "proxy-user" + "=" + URLEncoder.encode(user, "ASCII")); } client.executeMethod(config, m); // Process the response int statusCode = m.getStatusCode(); // Pass the response code. This method with the "reason phrase" is //deprecated but it's the only way to pass the reason along too. //noinspection deprecation servletResponse.setStatus(statusCode, m.getStatusLine().getReasonPhrase()); copyResponseHeaders(m, servletRequest, servletResponse); // Send the content to the client copyResponseEntity(m, servletResponse); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } if (e instanceof ServletException) { throw (ServletException) e; } //noinspection ConstantConditions if (e instanceof IOException) { throw (IOException) e; } throw new RuntimeException(e); } }
From source file:com.zimbra.cs.account.auth.HostedAuth.java
/** * zmprov md test.com zimbraAuthMech 'custom:hosted http://auth.customer.com:80' * //from w w w . j av a 2 s .c om * This custom auth module takes arguments in the following form: * {URL} [GET|POST - default is GET] [encryption method - defautl is plain] [auth protocol - default is imap] * e.g.: http://auth.customer.com:80 GET **/ public void authenticate(Account acct, String password, Map<String, Object> context, List<String> args) throws Exception { HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient(); HttpMethod method = null; String targetURL = args.get(0); /* if (args.size()>2) { authMethod = args.get(2); } if (args.size()>3) { authMethod = args.get(3); }*/ if (args.size() > 1) { if (args.get(1).equalsIgnoreCase("GET")) method = new GetMethod(targetURL); else method = new PostMethod(targetURL); } else method = new GetMethod(targetURL); if (context.get(AuthContext.AC_ORIGINATING_CLIENT_IP) != null) method.addRequestHeader(HEADER_CLIENT_IP, context.get(AuthContext.AC_ORIGINATING_CLIENT_IP).toString()); if (context.get(AuthContext.AC_REMOTE_IP) != null) method.addRequestHeader(HEADER_X_ZIMBRA_REMOTE_ADDR, context.get(AuthContext.AC_REMOTE_IP).toString()); method.addRequestHeader(HEADER_AUTH_USER, acct.getName()); method.addRequestHeader(HEADER_AUTH_PASSWORD, password); AuthContext.Protocol proto = (AuthContext.Protocol) context.get(AuthContext.AC_PROTOCOL); if (proto != null) method.addRequestHeader(HEADER_AUTH_PROTOCOL, proto.toString()); if (context.get(AuthContext.AC_USER_AGENT) != null) method.addRequestHeader(HEADER_AUTH_USER_AGENT, context.get(AuthContext.AC_USER_AGENT).toString()); try { HttpClientUtil.executeMethod(client, method); } catch (HttpException ex) { throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), acct.getName(), "HTTP request to remote authentication server failed", ex); } catch (IOException ex) { throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), acct.getName(), "HTTP request to remote authentication server failed", ex); } finally { if (method != null) method.releaseConnection(); } int status = method.getStatusCode(); if (status != HttpStatus.SC_OK) { throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), "HTTP request to remote authentication server failed. Remote response code: " + Integer.toString(status)); } String responseMessage; if (method.getResponseHeader(HEADER_AUTH_STATUS) != null) { responseMessage = method.getResponseHeader(HEADER_AUTH_STATUS).getValue(); } else { throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), "Empty response from remote authentication server."); } if (responseMessage.equalsIgnoreCase(AUTH_STATUS_OK)) { return; } else { throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), responseMessage); } }