List of usage examples for org.apache.commons.httpclient HttpMethod setRequestHeader
public abstract void setRequestHeader(String paramString1, String paramString2);
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 va 2 s . c om*/ } 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:com.adobe.share.api.ShareAPI.java
/** * Prepares a new HTTP request./*from w ww . ja va2 s. com*/ * * @param user the user * @param method the method * @param url the url * @param anon the anon * @param requestBody the request body * * @return the http method */ protected final HttpMethod createRequest(final ShareAPIUser user, final String method, final String url, final boolean anon, final String requestBody) { HttpMethod httpMethod = null; if ("GET".equals(method)) { httpMethod = new GetMethod(url); } else if ("POST".equals(method)) { httpMethod = new PostMethod(url); httpMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); try { ((PostMethod) httpMethod).setRequestEntity(new StringRequestEntity(requestBody, null, null)); } catch (UnsupportedEncodingException uee) { // Catch the unsupported encoding exception LOGGER.error("Unsupported encoding exception: " + uee.getMessage()); } } else if ("PUT".equals(method)) { httpMethod = new PutMethod(url); httpMethod.setRequestHeader("Content-Type", "text/plain"); if (requestBody != null) { ((PutMethod) httpMethod).setRequestEntity( new InputStreamRequestEntity(new ByteArrayInputStream(requestBody.getBytes()))); } } else if ("DELETE".equals(method)) { httpMethod = new DeleteMethod(url); } else if ("HEAD".equals(method)) { httpMethod = new HeadMethod(url); } /** * MoveMethod not supported by HttpClient else if("MOVE".equals(method)) * { httpMethod = new MoveMethod(url); } **/ httpMethod.setRequestHeader("Authorization", generateAuthorization(user, anon, method, url)); return httpMethod; }
From source file:com.zimbra.cs.servlet.ZimbraServlet.java
public static void proxyServletRequest(HttpServletRequest req, HttpServletResponse resp, HttpMethod method, HttpState state) throws IOException, ServiceException { // create an HTTP client with the same cookies javax.servlet.http.Cookie cookies[] = req.getCookies(); String hostname = method.getURI().getHost(); boolean hasZMAuth = hasZimbraAuthCookie(state); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals(ZimbraCookie.COOKIE_ZM_AUTH_TOKEN) && hasZMAuth) continue; state.addCookie(/* w w w .java 2s . c o m*/ new Cookie(hostname, cookies[i].getName(), cookies[i].getValue(), "/", null, false)); } } HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient(); if (state != null) client.setState(state); int hopcount = 0; for (Enumeration<?> enm = req.getHeaderNames(); enm.hasMoreElements();) { String hname = (String) enm.nextElement(), hlc = hname.toLowerCase(); if (hlc.equals("x-zimbra-hopcount")) try { hopcount = Math.max(Integer.parseInt(req.getHeader(hname)), 0); } catch (NumberFormatException e) { } else if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.equals("authorization")) method.addRequestHeader(hname, req.getHeader(hname)); } if (hopcount >= MAX_PROXY_HOPCOUNT) throw ServiceException.TOO_MANY_HOPS(HttpUtil.getFullRequestURL(req)); method.addRequestHeader("X-Zimbra-Hopcount", Integer.toString(hopcount + 1)); if (method.getRequestHeader("X-Zimbra-Orig-Url") == null) method.addRequestHeader("X-Zimbra-Orig-Url", req.getRequestURL().toString()); String ua = req.getHeader("User-Agent"); if (ua != null) method.setRequestHeader("User-Agent", ua); // dispatch the request and copy over the results int statusCode = -1; for (int retryCount = 3; statusCode == -1 && retryCount > 0; retryCount--) { statusCode = HttpClientUtil.executeMethod(client, method); } if (statusCode == -1) { resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "retry limit reached"); return; } else if (statusCode >= 300) { resp.sendError(statusCode, method.getStatusText()); return; } Header[] headers = method.getResponseHeaders(); for (int i = 0; i < headers.length; i++) { String hname = headers[i].getName(), hlc = hname.toLowerCase(); if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.startsWith("www-")) resp.addHeader(hname, headers[i].getValue()); } InputStream responseStream = method.getResponseBodyAsStream(); if (responseStream == null || resp.getOutputStream() == null) return; ByteUtil.copy(method.getResponseBodyAsStream(), false, resp.getOutputStream(), false); }
From source file:com.sun.faban.driver.transport.hc3.ApacheHC3Transport.java
/** * Sets the request header. If there are multiple values for this header, * use a comma-separated list for the values. * @param method The HttpMethod/*from w w w . j ava 2s . c om*/ * @param headers The request headers */ private void setHeaders(HttpMethod method, Map<String, String> headers) { if (headers == null) { method.setRequestHeader("Accept-Language", "en-us,en;q=0.5"); return; } else if (!headers.containsKey("Accept-Language")) { method.setRequestHeader("Accept-Language", "en-us,en;q=0.5"); } for (Map.Entry<String, String> entry : headers.entrySet()) method.setRequestHeader(entry.getKey(), entry.getValue()); }
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;//w w w . ja v a2s .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.rometools.fetcher.impl.HttpClientFeedFetcher.java
@Override public SyndFeed retrieveFeed(final String userAgent, final URL feedUrl) throws IllegalArgumentException, IOException, FeedException, FetcherException { if (feedUrl == null) { throw new IllegalArgumentException("null is not a valid URL"); }/*from ww w . ja va 2 s.co m*/ final HttpClient client = new HttpClient(httpClientParams); if (credentialSupplier != null) { final HttpClientParams params = client.getParams(); params.setAuthenticationPreemptive(true); final String host = feedUrl.getHost(); final Credentials credentials = credentialSupplier.getCredentials(null, host); if (credentials != null) { final AuthScope authScope = new AuthScope(host, -1); final HttpState state = client.getState(); state.setCredentials(authScope, credentials); } } System.setProperty("httpclient.useragent", userAgent); final String urlStr = feedUrl.toString(); final HttpMethod method = new GetMethod(urlStr); if (customRequestHeaders == null) { method.addRequestHeader("Accept-Encoding", "gzip"); method.addRequestHeader("User-Agent", userAgent); } else { for (final Map.Entry<String, String> entry : customRequestHeaders.entrySet()) { method.addRequestHeader(entry.getKey(), entry.getValue()); } if (!customRequestHeaders.containsKey("Accept-Encoding")) { method.addRequestHeader("Accept-Encoding", "gzip"); } if (!customRequestHeaders.containsKey("User-Agent")) { method.addRequestHeader("User-Agent", userAgent); } } method.setFollowRedirects(true); if (httpClientMethodCallback != null) { synchronized (httpClientMethodCallback) { httpClientMethodCallback.afterHttpClientMethodCreate(method); } } final FeedFetcherCache cache = getFeedInfoCache(); if (cache != null) { // retrieve feed try { if (isUsingDeltaEncoding()) { method.setRequestHeader("A-IM", "feed"); } // try to get the feed info from the cache SyndFeedInfo syndFeedInfo = cache.getFeedInfo(feedUrl); if (syndFeedInfo != null) { method.setRequestHeader("If-None-Match", syndFeedInfo.getETag()); final Object lastModifiedHeader = syndFeedInfo.getLastModified(); if (lastModifiedHeader instanceof String) { method.setRequestHeader("If-Modified-Since", (String) lastModifiedHeader); } } final int statusCode = client.executeMethod(method); fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr); handleErrorCodes(statusCode); SyndFeed feed = getFeed(syndFeedInfo, urlStr, method, statusCode); syndFeedInfo = buildSyndFeedInfo(feedUrl, urlStr, method, feed, statusCode); cache.setFeedInfo(feedUrl, syndFeedInfo); // the feed may have been modified to pick up cached values // (eg - for delta encoding) feed = syndFeedInfo.getSyndFeed(); return feed; } finally { method.releaseConnection(); } } else { // cache is not in use try { final int statusCode = client.executeMethod(method); fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr); handleErrorCodes(statusCode); return getFeed(null, urlStr, method, statusCode); } finally { method.releaseConnection(); } } }
From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java
private void setAcceptHeaders(CrawlURI curi, HttpMethod get) { try {//from ww w . j a va 2s . c om StringList accept_headers = (StringList) getAttribute(ATTR_ACCEPT_HEADERS, curi); if (!accept_headers.isEmpty()) { for (ListIterator i = accept_headers.listIterator(); i.hasNext();) { String hdr = (String) i.next(); String[] nvp = hdr.split(": +"); if (nvp.length == 2) { get.setRequestHeader(nvp[0], nvp[1]); } else { logger.warning("Invalid accept header: " + hdr); } } } } catch (AttributeNotFoundException e) { logger.severe(e.getMessage()); } }
From source file:com.cloudbees.api.BeesClient.java
/** * Executes an HTTP method./* w ww. j ava 2s . c o m*/ */ private HttpReply executeRequest(HttpMethod httpMethod, Map<String, String> headers) throws IOException { BeesClientConfiguration conf = getBeesClientConfiguration(); HttpClient httpClient = HttpClientHelper.createClient(conf); if (encodedAccountAuthorization != null) httpMethod.setRequestHeader("Authorization", encodedAccountAuthorization); if (headers != null) { for (Map.Entry<String, String> entry : headers.entrySet()) { httpMethod.setRequestHeader(entry.getKey(), entry.getValue()); } } int status = 500; String rsp = "Error"; try { status = httpClient.executeMethod(httpMethod); rsp = IOUtils.toString(httpMethod.getResponseBodyAsStream()); } catch (IOException e) { throw (IOException) new IOException("Failed to " + httpMethod.getName() + " : " + httpMethod.getURI() + " : code=" + status + " response=" + e.getMessage()).initCause(e); } finally { httpMethod.releaseConnection(); } trace(status + ": " + rsp); return new HttpReply(httpMethod, status, rsp); }
From source file:io.hops.hopsworks.api.jobs.JobService.java
/** * Get the job ui for the specified job. * This act as a proxy to get the job ui from yarn * <p>/* w w w .j a va 2s. c o m*/ * @param appId * @param param * @param sc * @param req * @return */ @GET @Path("/{appId}/prox/{path: .+}") @Produces(MediaType.WILDCARD) @AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST }) public Response getProxy(@PathParam("appId") final String appId, @PathParam("path") final String param, @Context SecurityContext sc, @Context HttpServletRequest req) { Response response = checkAccessRight(appId); if (response != null) { return response; } try { String trackingUrl; if (param.matches("http([a-zA-Z,:,/,.,0-9,-])+:([0-9])+(.)+")) { trackingUrl = param; } else { trackingUrl = "http://" + param; } trackingUrl = trackingUrl.replace("@hwqm", "?"); if (!hasAppAccessRight(trackingUrl)) { LOGGER.log(Level.SEVERE, "A user is trying to access an app outside their project!"); return Response.status(Response.Status.FORBIDDEN).build(); } org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(trackingUrl, false); HttpClientParams params = new HttpClientParams(); params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); HttpClient client = new HttpClient(params); final HttpMethod method = new GetMethod(uri.getEscapedURI()); Enumeration<String> names = req.getHeaderNames(); while (names.hasMoreElements()) { String name = names.nextElement(); String value = req.getHeader(name); if (PASS_THROUGH_HEADERS.contains(name)) { //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 (!name.toLowerCase().equals("accept-encoding") || trackingUrl.contains(".js")) { method.setRequestHeader(name, value); } } } String user = req.getRemoteUser(); if (user != null && !user.isEmpty()) { method.setRequestHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII")); } client.executeMethod(method); Response.ResponseBuilder responseBuilder = noCacheResponse .getNoCacheResponseBuilder(Response.Status.OK); for (Header header : method.getResponseHeaders()) { responseBuilder.header(header.getName(), header.getValue()); } //method.getPath().contains("/allexecutors") is needed to replace the links under Executors tab //which are in a json response object if (method.getResponseHeader("Content-Type") == null || method.getResponseHeader("Content-Type").getValue().contains("html") || method.getPath().contains("/allexecutors")) { final String source = "http://" + method.getURI().getHost() + ":" + method.getURI().getPort(); if (method.getResponseHeader("Content-Length") == null) { responseBuilder.entity(new StreamingOutput() { @Override public void write(OutputStream out) throws IOException, WebApplicationException { Writer writer = new BufferedWriter(new OutputStreamWriter(out)); InputStream stream = method.getResponseBodyAsStream(); Reader in = new InputStreamReader(stream, "UTF-8"); char[] buffer = new char[4 * 1024]; String remaining = ""; int n; while ((n = in.read(buffer)) != -1) { StringBuilder strb = new StringBuilder(); strb.append(buffer, 0, n); String s = remaining + strb.toString(); remaining = s.substring(s.lastIndexOf(">") + 1, s.length()); s = hopify(s.substring(0, s.lastIndexOf(">") + 1), param, appId, source); writer.write(s); } writer.flush(); } }); } else { String s = hopify(method.getResponseBodyAsString(), param, appId, source); responseBuilder.entity(s); responseBuilder.header("Content-Length", s.length()); } } else { responseBuilder.entity(new StreamingOutput() { @Override public void write(OutputStream out) throws IOException, WebApplicationException { InputStream stream = method.getResponseBodyAsStream(); org.apache.hadoop.io.IOUtils.copyBytes(stream, out, 4096, true); out.flush(); } }); } return responseBuilder.build(); } catch (Exception e) { LOGGER.log(Level.SEVERE, "exception while geting job ui " + e.getLocalizedMessage(), e); return noCacheResponse.getNoCacheResponseBuilder(Response.Status.NOT_FOUND).build(); } }
From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java
/** * Set the given conditional-GET header, if the setting is enabled and * a suitable value is available in the URI history. * @param curi source CrawlURI/* ww w . j a v a2 s .co m*/ * @param method HTTP operation pending * @param setting true/false enablement setting name to consult * @param sourceHeader header to consult in URI history * @param targetHeader header to set if possible */ protected void setConditionalGetHeader(CrawlURI curi, HttpMethod method, String setting, String sourceHeader, String targetHeader) { if (((Boolean) getUncheckedAttribute(curi, setting))) { try { int previousStatus = curi.getAList().getAListArray(A_FETCH_HISTORY)[0].getInt(A_STATUS); if (previousStatus <= 0) { // do not reuse headers from any broken fetch return; } String previousValue = curi.getAList().getAListArray(A_FETCH_HISTORY)[0].getString(sourceHeader); if (previousValue != null) { method.setRequestHeader(targetHeader, previousValue); } } catch (RuntimeException e) { // for absent key, bad index, etc. just do nothing } } }