private HttpMethodParams httpParams(Map<String, String> params) { HttpMethodParams methodParams = new HttpMethodParams(); for (String param : params.keySet()) { methodParams.setParameter(param, params.get(param)); } return methodParams; }
/** Handles the HTTP post. Throws HttpException on failure */ @SuppressWarnings("deprecation") private void doPost(PostMethod method, RequestEntity data, String dest) throws IOException, HttpException { HttpMethodParams pars = method.getParams(); pars.setParameter( HttpMethodParams.RETRY_HANDLER, (Object) new HttpMethodRetryHandler() { public boolean retryMethod(HttpMethod m, IOException e, int exec) { return !(e instanceof java.net.ConnectException) && (exec < MAX_RETRIES_PER_COLLECTOR); } }); method.setParams(pars); method.setPath(dest); // send it across the network method.setRequestEntity(data); log.info("HTTP post to " + dest + " length = " + data.getContentLength()); // Send POST request client.setTimeout(8000); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { log.error( "HTTP post response statusCode: " + statusCode + ", statusLine: " + method.getStatusLine()); // do something aggressive here throw new HttpException("got back a failure from server"); } // implicitly "else" log.info( "got success back from the remote collector; response length " + method.getResponseContentLength()); // FIXME: should parse acks here InputStream rstream = null; // Get the response body byte[] resp_buf = method.getResponseBody(); rstream = new ByteArrayInputStream(resp_buf); BufferedReader br = new BufferedReader(new InputStreamReader(rstream)); String line; while ((line = br.readLine()) != null) { System.out.println("response: " + line); } }
/** * 주어진 url의 응답 결과를 얻는다. * * @param url * @param params * @return HttpResult */ protected HttpResult getHttp(String url, Map<String, String> params) { HttpResult result = new HttpResult(); HttpClient client = getHttpClient(); HttpMethod method = new GetMethod(url); if (params != null) { HttpMethodParams param = new HttpMethodParams(); for (String key : params.keySet()) { param.setParameter(key, params.get(key)); } method.setParams(param); } try { int statusCode = client.executeMethod(method); result.setStatusCode(statusCode); if (statusCode == HttpStatus.SC_OK) { // 동적페이지의 경우 last-modified 는 없음. InputStream bodyAsStream = method.getResponseBodyAsStream(); StringBuilder sb = new StringBuilder(); byte[] b = new byte[1024]; for (int n; (n = bodyAsStream.read(b)) != -1; ) { sb.append(new String(b, 0, n, this.getEnconding())); } result.setContent(sb.toString()); } } catch (Exception e) { logger.error(e.getMessage(), e); } return result; }
@Override public void executeMethod(final HttpMethod method, final ClientRequest cr) { final Map<String, Object> props = cr.getProperties(); method.setDoAuthentication(true); final HttpMethodParams methodParams = method.getParams(); // Set the handle cookies property if (!cr.getPropertyAsFeature(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES)) { methodParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); } // Set the interactive and credential provider properties if (cr.getPropertyAsFeature(ApacheHttpClientConfig.PROPERTY_INTERACTIVE)) { CredentialsProvider provider = (CredentialsProvider) props.get(ApacheHttpClientConfig.PROPERTY_CREDENTIALS_PROVIDER); if (provider == null) { provider = DEFAULT_CREDENTIALS_PROVIDER; } methodParams.setParameter(CredentialsProvider.PROVIDER, provider); } else { methodParams.setParameter(CredentialsProvider.PROVIDER, null); } // Set the read timeout final Integer readTimeout = (Integer) props.get(ApacheHttpClientConfig.PROPERTY_READ_TIMEOUT); if (readTimeout != null) { methodParams.setSoTimeout(readTimeout); } if (method instanceof EntityEnclosingMethod) { final EntityEnclosingMethod entMethod = (EntityEnclosingMethod) method; if (cr.getEntity() != null) { final RequestEntityWriter re = getRequestEntityWriter(cr); final Integer chunkedEncodingSize = (Integer) props.get(ApacheHttpClientConfig.PROPERTY_CHUNKED_ENCODING_SIZE); if (chunkedEncodingSize != null) { // There doesn't seems to be a way to set the chunk size. entMethod.setContentChunked(true); // It is not possible for a MessageBodyWriter to modify // the set of headers before writing out any bytes to // the OutputStream // This makes it impossible to use the multipart // writer that modifies the content type to add a boundary // parameter writeOutBoundHeaders(cr.getHeaders(), method); // Do not buffer the request entity when chunked encoding is // set entMethod.setRequestEntity( new RequestEntity() { @Override public boolean isRepeatable() { return false; } @Override public void writeRequest(OutputStream out) throws IOException { re.writeRequestEntity(out); } @Override public long getContentLength() { return re.getSize(); } @Override public String getContentType() { return re.getMediaType().toString(); } }); } else { entMethod.setContentChunked(false); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { re.writeRequestEntity( new CommittingOutputStream(baos) { @Override protected void commit() throws IOException { writeOutBoundHeaders(cr.getMetadata(), method); } }); } catch (IOException ex) { throw new ClientHandlerException(ex); } final byte[] content = baos.toByteArray(); entMethod.setRequestEntity( new RequestEntity() { @Override public boolean isRepeatable() { return true; } @Override public void writeRequest(OutputStream out) throws IOException { out.write(content); } @Override public long getContentLength() { return content.length; } @Override public String getContentType() { return re.getMediaType().toString(); } }); } } } else { writeOutBoundHeaders(cr.getHeaders(), method); // Follow redirects method.setFollowRedirects( cr.getPropertyAsFeature(ApacheHttpClientConfig.PROPERTY_FOLLOW_REDIRECTS)); } try { httpClient.executeMethod( getHostConfiguration(httpClient, props), method, getHttpState(props)); } catch (Exception e) { method.releaseConnection(); throw new ClientHandlerException(e); } }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { GetMethod httpGet = null; try { String url = RequestUtil.getParameter(request, PARAM_URL, defaultProxyUrl); String host = url.split("/")[2]; // Get the proxy parameters // TODO: Add dependency injection to set proxy config from GeoNetwork settings, using also the // credentials configured String proxyHost = System.getProperty("http.proxyHost"); String proxyPort = System.getProperty("http.proxyPort"); // Get rest of parameters to pass to proxied url HttpMethodParams urlParams = new HttpMethodParams(); Enumeration paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); if (!paramName.equalsIgnoreCase(PARAM_URL)) { urlParams.setParameter(paramName, request.getParameter(paramName)); } } // Checks if allowed host if (!isAllowedHost(host)) { // throw new ServletException("This proxy does not allow you to access that location."); returnExceptionMessage(response, "This proxy does not allow you to access that location."); return; } if (url.startsWith("http://") || url.startsWith("https://")) { HttpClient client = new HttpClient(); // Added support for proxy if (proxyHost != null && proxyPort != null) { client.getHostConfiguration().setProxy(proxyHost, Integer.valueOf(proxyPort)); } httpGet = new GetMethod(url); httpGet.setParams(urlParams); client.executeMethod(httpGet); if (httpGet.getStatusCode() == HttpStatus.SC_OK) { Header contentType = httpGet.getResponseHeader(HEADER_CONTENT_TYPE); String[] contentTypesReturned = contentType.getValue().split(";"); if (!isValidContentType(contentTypesReturned[0])) { contentTypesReturned = contentType.getValue().split(" "); if (!isValidContentType(contentTypesReturned[0])) { throw new ServletException("Status: 415 Unsupported media type"); } } // Sets response contentType response.setContentType(getResponseContentType(contentTypesReturned)); String responseBody = httpGet.getResponseBodyAsString().trim(); PrintWriter out = response.getWriter(); out.print(responseBody); out.flush(); out.close(); } else { returnExceptionMessage( response, "Unexpected failure: " + httpGet.getStatusLine().toString()); } httpGet.releaseConnection(); } else { // throw new ServletException("only HTTP(S) protocol supported"); returnExceptionMessage(response, "only HTTP(S) protocol supported"); } } catch (Exception e) { e.printStackTrace(); // throw new ServletException("Some unexpected error occurred. Error text was: " + // e.getMessage()); returnExceptionMessage( response, "Some unexpected error occurred. Error text was: " + e.getMessage()); } finally { if (httpGet != null) httpGet.releaseConnection(); } }