List of usage examples for javax.servlet.http HttpServletRequest getHeaders
public Enumeration<String> getHeaders(String name);
Enumeration
of String
objects. From source file:com.fujitsu.dc.engine.rs.AbstractService.java
/** * Service./* w w w. j a v a 2 s . c o m*/ * @param cell Cell?? * @param scheme URI * @param svcName ??? * @param req Request * @param res Response * @param is * @return Response */ public final Response run(final String cell, final String scheme, final String svcName, final HttpServletRequest req, final HttpServletResponse res, final InputStream is) { StringBuilder msg = new StringBuilder(); msg.append("[" + DcEngineConfig.getVersion() + "] " + ">>> Request Started "); msg.append(" method:"); msg.append(req.getMethod()); msg.append(" method:"); msg.append(req.getRequestURL()); msg.append(" url:"); msg.append(cell); msg.append(" scheme:"); msg.append(scheme); msg.append(" svcName:"); msg.append(svcName); log.info(msg); // ? ???? Enumeration<String> multiheaders = req.getHeaderNames(); for (String headerName : Collections.list(multiheaders)) { Enumeration<String> headers = req.getHeaders(headerName); for (String header : Collections.list(headers)) { log.debug("RequestHeader['" + headerName + "'] = " + header); } } this.setServiceName(svcName); // ?URL?? String targetCell = cell; if (cell == null) { targetCell = getCell(); } String targetScheme = scheme; if (scheme == null) { targetScheme = getScheme(); } String targetServiceName = svcName; String baseUrl; try { baseUrl = parseRequestUri(req, res); } catch (MalformedURLException e) { // URL??????? return makeErrorResponse("Server Error", DcEngineException.STATUSCODE_SERVER_ERROR); } Response response = null; DcEngineContext dcContext = null; try { try { dcContext = new DcEngineContext(); } catch (DcEngineException e) { return errorResponse(e); } // ??? try { this.sourceManager = this.getServiceCollectionManager(); dcContext.setSourceManager(this.sourceManager); this.serviceSubject = this.sourceManager.getServiceSubject(); } catch (DcEngineException e) { return errorResponse(e); } // ?? dcContext.loadGlobalObject(baseUrl, targetCell, targetScheme, targetScheme, targetServiceName); // ??? String source = ""; try { String sourceName = this.sourceManager.getScriptNameForServicePath(targetServiceName); source = this.sourceManager.getSource(sourceName); } catch (DcEngineException e) { return errorResponse(e); } catch (Exception e) { log.info("User Script not found to targetCell(" + targetCell + ", targetScheme(" + targetScheme + "), targetServiceName(" + targetServiceName + ")"); log.info(e.getMessage(), e); return errorResponse(new DcEngineException("404 Not Found (User Script)", DcEngineException.STATUSCODE_NOTFOUND)); } // JSGI try { response = dcContext.runJsgi(source, req, res, is, this.serviceSubject); } catch (DcEngineException e) { return errorResponse(e); } catch (Exception e) { log.warn(" unknown Exception(" + e.getMessage() + ")"); return errorResponse(new DcEngineException("404 Not Found (Service Excute Error)", DcEngineException.STATUSCODE_NOTFOUND)); } } finally { IOUtils.closeQuietly(dcContext); } return response; }
From source file:flex.messaging.services.http.proxy.SecurityFilter.java
private void setCredentials(ProxyContext context) { String user = null, password = null; // Check for credentials in runAs user = context.getTarget().getRemoteUsername(); password = context.getTarget().getRemotePassword(); String fromRequest = null;// ww w . jav a 2 s .c om HttpServletRequest clientRequest = FlexContext.getHttpRequest(); if (clientRequest != null) { // Check for credentials in parameter/header fromRequest = clientRequest.getHeader(ProxyConstants.HEADER_CREDENTIALS); } // We sometimes send the credentials as a URL parameter to work around a player/Opera issue if (fromRequest == null) { fromRequest = context.getCredentialsHeader(); } // Add authentication header for credentials if (fromRequest != null) { Base64.Decoder decoder = new Base64.Decoder(); decoder.decode(fromRequest); String decoded = new String(decoder.drain()); int colonIdx = decoded.indexOf(":"); if (colonIdx != -1) { user = decoded.substring(0, colonIdx); } password = decoded.substring(colonIdx + 1); } // Check for existing authentication header if (clientRequest != null) { Enumeration headers = clientRequest.getHeaders("Authorization"); if (headers != null) { while (headers.hasMoreElements()) { String value = (String) headers.nextElement(); if (value.startsWith("Basic")) { if (!context.isLocalDomainAndPort()) { if (Log.isInfo()) { Log.getLogger(HTTPProxyService.LOG_CATEGORY) .debug("Not sending on Authentication header. Proxy domain:port of " + clientRequest.getServerName() + ":" + clientRequest.getServerPort() + " does not match target domain:port of " + context.getTarget().getUrl().getHost() + ":" + context.getTarget().getUrl().getPort()); } } else { // Super gross hack to work around what appears to be an commons-httpclient bug // where headers are not resent after a 302. Base64.Decoder decoder = new Base64.Decoder(); String encoded = value.substring(6); decoder.decode(encoded); String decoded = new String(decoder.drain()); int colonIdx = decoded.indexOf(":"); user = decoded.substring(0, colonIdx); password = decoded.substring(colonIdx + 1); } } } } } // Set up request for authentication if (user != null) { UsernamePasswordCredentials cred = new UsernamePasswordCredentials(user, password); context.getHttpClient().getState().setCredentials(ProxyUtil.getDefaultAuthScope(), cred); context.setAuthorization(true); if (Log.isInfo()) { Log.getLogger(HTTPProxyService.LOG_CATEGORY) .info("-- Authentication header being sent for " + user); } } }
From source file:org.apache.click.extras.filter.PerformanceFilter.java
/** * Return true if the response should be GZIP compressed. * * @param request the request to test/*from w ww . ja va 2s . c o m*/ * @param response the response to test * @param path the request path to test * @return true if the response should be GZIP compressed */ protected boolean useGzipCompression(HttpServletRequest request, HttpServletResponse response, String path) { if (!compressionEnabled) { return false; } // If Content-Encoding header is already set on response, skip compression if (response.containsHeader("Content-Encoding")) { return false; } if (compressionThreshold > 0) { path = path.toLowerCase(); if (path.endsWith(".gif") || path.endsWith(".png") || path.endsWith(".jpg")) { return false; } Enumeration<?> e = request.getHeaders("Accept-Encoding"); while (e.hasMoreElements()) { String name = (String) e.nextElement(); if (name.indexOf("gzip") != -1) { return true; } } } return false; }
From source file:io.personium.engine.rs.AbstractService.java
/** * Service./*from w ww.jav a 2 s. c o m*/ * @param cell Cell?? * @param schema URI * @param svcName ??? * @param req Request * @param res Response * @param is * @return Response */ public final Response run(final String cell, final String schema, final String svcName, final HttpServletRequest req, final HttpServletResponse res, final InputStream is) { StringBuilder msg = new StringBuilder(); msg.append("[" + PersoniumEngineConfig.getVersion() + "] " + ">>> Request Started "); msg.append(" method:"); msg.append(req.getMethod()); msg.append(" method:"); msg.append(req.getRequestURL()); msg.append(" url:"); msg.append(cell); msg.append(" schema:"); msg.append(schema); msg.append(" svcName:"); msg.append(svcName); log.info(msg); // ? ???? Enumeration<String> multiheaders = req.getHeaderNames(); for (String headerName : Collections.list(multiheaders)) { Enumeration<String> headers = req.getHeaders(headerName); for (String header : Collections.list(headers)) { log.debug("RequestHeader['" + headerName + "'] = " + header); } } this.setServiceName(svcName); // ?URL?? String targetCell = cell; if (cell == null) { targetCell = getCell(); } String targetSchema = schema; if (schema == null) { targetSchema = getSchemaURI(); } String targetServiceName = svcName; String baseUrl; try { baseUrl = parseRequestUri(req, res); } catch (MalformedURLException e) { // URL??????? return makeErrorResponse("Server Error", PersoniumEngineException.STATUSCODE_SERVER_ERROR); } Response response = null; PersoniumEngineContext pecx = null; try { try { pecx = new PersoniumEngineContext(); } catch (PersoniumEngineException e) { return errorResponse(e); } // ??? try { this.sourceManager = this.getServiceCollectionManager(); pecx.setSourceManager(this.sourceManager); this.serviceSubject = this.sourceManager.getServiceSubject(); } catch (PersoniumEngineException e) { return errorResponse(e); } // ?? pecx.loadGlobalObject(baseUrl, targetCell, targetSchema, targetSchema, targetServiceName); // ??? String source = ""; try { String sourceName = this.sourceManager.getScriptNameForServicePath(targetServiceName); source = this.sourceManager.getSource(sourceName); } catch (PersoniumEngineException e) { return errorResponse(e); } catch (Exception e) { log.info("User Script not found to targetCell(" + targetCell + ", targetschema(" + targetSchema + "), targetServiceName(" + targetServiceName + ")"); log.info(e.getMessage(), e); return errorResponse(new PersoniumEngineException("404 Not Found (User Script)", PersoniumEngineException.STATUSCODE_NOTFOUND)); } // JSGI try { response = pecx.runJsgi(source, req, res, is, this.serviceSubject); } catch (PersoniumEngineException e) { return errorResponse(e); } catch (Exception e) { log.warn(" unknown Exception(" + e.getMessage() + ")"); return errorResponse(new PersoniumEngineException("404 Not Found (Service Execute Error)", PersoniumEngineException.STATUSCODE_NOTFOUND)); } } finally { IOUtils.closeQuietly(pecx); } return response; }
From source file:httpmultiplexer.httpproxy.ProxyServlet.java
/** * Copy request headers from the servlet client to the proxy request. *///from w w w. j a va2 s. c o m protected void copyRequestHeaders(HttpServletRequest servletRequest, HttpRequest proxyRequest) { // Get an Enumeration of all of the header names sent by the client Enumeration enumerationOfHeaderNames = servletRequest.getHeaderNames(); while (enumerationOfHeaderNames.hasMoreElements()) { String headerName = (String) enumerationOfHeaderNames.nextElement(); //Instead the content-length is effectively set via InputStreamEntity if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) { continue; } if (hopByHopHeaders.containsHeader(headerName)) { continue; } Enumeration headers = servletRequest.getHeaders(headerName); while (headers.hasMoreElements()) {//sometimes more than one value String headerValue = (String) headers.nextElement(); // In case the proxy host is running multiple virtual servers, // rewrite the Host header to ensure that we get content from // the correct virtual server if (headerName.equalsIgnoreCase(HttpHeaders.HOST)) { HttpHost host = getTargetHost(servletRequest); headerValue = host.getHostName(); if (host.getPort() != -1) { headerValue += ":" + host.getPort(); } } proxyRequest.addHeader(headerName, headerValue); } } }
From source file:org.fcrepo.server.access.FedoraAccessServlet.java
public void getDatastreamDissemination(Context context, String PID, String dsID, Date asOfDateTime, HttpServletResponse response, HttpServletRequest request) throws IOException, ServerException { ServletOutputStream out = null;/*from ww w . j av a2 s .c om*/ MIMETypedStream dissemination = null; dissemination = m_access.getDatastreamDissemination(context, PID, dsID, asOfDateTime); try { // testing to see what's in request header that might be of interest if (logger.isDebugEnabled()) { for (Enumeration<?> e = request.getHeaderNames(); e.hasMoreElements();) { String name = (String) e.nextElement(); Enumeration<?> headerValues = request.getHeaders(name); StringBuffer sb = new StringBuffer(); while (headerValues.hasMoreElements()) { sb.append((String) headerValues.nextElement()); } String value = sb.toString(); logger.debug("FEDORASERVLET REQUEST HEADER CONTAINED: {} : {}", name, value); } } // Dissemination was successful; // Return MIMETypedStream back to browser client if (dissemination.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) { String location = ""; for (Property prop : dissemination.header) { if (prop.name.equalsIgnoreCase(HttpHeaders.LOCATION)) { location = prop.value; break; } } response.sendRedirect(location); } else { int status = dissemination.getStatusCode(); response.setStatus(status); if (status == HttpStatus.SC_OK) { response.setContentType(dissemination.getMIMEType()); } Property[] headerArray = dissemination.header; if (headerArray != null) { for (int i = 0; i < headerArray.length; i++) { if (headerArray[i].name != null && !headerArray[i].name.equalsIgnoreCase("transfer-encoding") && !headerArray[i].name.equalsIgnoreCase("content-type")) { response.addHeader(headerArray[i].name, headerArray[i].value); logger.debug( "THIS WAS ADDED TO FEDORASERVLET RESPONSE HEADER FROM ORIGINATING PROVIDER {} : {}", headerArray[i].name, headerArray[i].value); } } } out = response.getOutputStream(); int byteStream = 0; logger.debug("Started reading dissemination stream"); InputStream dissemResult = dissemination.getStream(); byte[] buffer = new byte[BUF]; while ((byteStream = dissemResult.read(buffer)) != -1) { out.write(buffer, 0, byteStream); } buffer = null; dissemResult.close(); dissemResult = null; out.flush(); out.close(); logger.debug("Finished reading dissemination stream"); } } finally { dissemination.close(); } }
From source file:com.nominanuda.web.http.ServletHelper.java
public HttpRequest copyRequest(HttpServletRequest servletRequest, boolean stripContextPath) throws IOException { final InputStream is = getServletRequestBody(servletRequest); String method = servletRequest.getMethod(); String uri = getRequestLineURI(servletRequest, stripContextPath); String ct = servletRequest.getContentType(); @SuppressWarnings("unused") String charenc = getCharacterEncoding(servletRequest); String cenc = getContentEncoding(servletRequest); long contentLength = servletRequest.getContentLength(); HttpRequest req;/*from w ww. j a va2 s . co m*/ if (is == null) { req = new BasicHttpRequest(method, uri); } else { req = new BasicHttpEntityEnclosingRequest(method, uri); HttpEntity entity = buildEntity(servletRequest, is, contentLength, ct, cenc); if (entity != null) { ((BasicHttpEntityEnclosingRequest) req).setEntity(entity); } } Enumeration<?> names = servletRequest.getHeaderNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); Enumeration<?> vals = servletRequest.getHeaders(name); while (vals.hasMoreElements()) { String value = (String) vals.nextElement(); req.addHeader(name, value); } } return req; }
From source file:io.hops.hopsworks.api.kibana.ProxyServlet.java
/** * Copy request headers from the servlet client to the proxy request. * * @param servletRequest/*from w w w . ja v a 2 s . com*/ * @param proxyRequest */ protected void copyRequestHeaders(HttpServletRequest servletRequest, HttpRequest proxyRequest) { // Get an Enumeration of all of the header names sent by the client Enumeration enumerationOfHeaderNames = servletRequest.getHeaderNames(); while (enumerationOfHeaderNames.hasMoreElements()) { String headerName = (String) enumerationOfHeaderNames.nextElement(); //Instead the content-length is effectively set via InputStreamEntity if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) { continue; } if (hopByHopHeaders.containsHeader(headerName)) { continue; } Enumeration headers = servletRequest.getHeaders(headerName); while (headers.hasMoreElements()) {//sometimes more than one value String headerValue = (String) headers.nextElement(); // In case the proxy host is running multiple virtual servers, // rewrite the Host header to ensure that we get content from // the correct virtual server if (headerName.equalsIgnoreCase(HttpHeaders.HOST)) { HttpHost host = getTargetHost(servletRequest); if (host != null) { headerValue = host.getHostName(); if (host.getPort() != -1) { headerValue += ":" + host.getPort(); } } } else if (headerName.equalsIgnoreCase(org.apache.http.cookie.SM.COOKIE)) { headerValue = getRealCookie(headerValue); } proxyRequest.addHeader(headerName, headerValue); } } }
From source file:com.tremolosecurity.proxy.filter.HttpFilterRequestImpl.java
public HttpFilterRequestImpl(HttpServletRequest request, AuthInfo authInfo) { this.request = request; this.headers = new HashMap<String, Attribute>(); this.cookies = new HashMap<String, ArrayList<Cookie>>(); this.params = new HashMap<String, Attribute>(); this.paramNames = new ArrayList<String>(); Enumeration enumer = request.getParameterNames(); while (enumer.hasMoreElements()) { String name = (String) enumer.nextElement(); this.paramNames.add(name); }//from ww w . j a v a2 s . co m this.authInfo = authInfo; boolean first = true; ProxyUtil.loadParams(request, this.params); enumer = request.getHeaderNames(); while (enumer.hasMoreElements()) { String name = (String) enumer.nextElement(); Enumeration enumerVals = request.getHeaders(name); Attribute attrib = new Attribute(name); this.headers.put(attrib.getName().toLowerCase(), attrib); while (enumerVals.hasMoreElements()) { attrib.getValues().add((String) enumerVals.nextElement()); } } Cookie[] cookies = request.getCookies(); if (cookies == null) { cookies = new Cookie[0]; } for (int i = 0; i < cookies.length; i++) { ArrayList<Cookie> cookieList = this.cookies.get(cookies[i].getName()); if (cookieList == null) { cookieList = new ArrayList<Cookie>(); this.cookies.put(cookies[i].getName(), cookieList); } cookieList.add(cookies[i]); } }
From source file:com.google.gwt.jolokia.server.servlet.ProxyServlet.java
/** Copy request headers from the servlet client to the proxy request. */ protected void copyRequestHeaders(HttpServletRequest servletRequest, HttpRequest proxyRequest) { // Get an Enumeration of all of the header names sent by the client Enumeration enumerationOfHeaderNames = servletRequest.getHeaderNames(); while (enumerationOfHeaderNames.hasMoreElements()) { String headerName = (String) enumerationOfHeaderNames.nextElement(); // Instead the content-length is effectively set via // InputStreamEntity if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) continue; if (hopByHopHeaders.containsHeader(headerName)) continue; Enumeration headers = servletRequest.getHeaders(headerName); while (headers.hasMoreElements()) {// sometimes more than one value String headerValue = (String) headers.nextElement(); // In case the proxy host is running multiple virtual servers, // rewrite the Host header to ensure that we get content from // the correct virtual server if (headerName.equalsIgnoreCase(HttpHeaders.HOST)) { HttpHost host = getTargetHost(servletRequest); headerValue = host.getHostName(); if (host.getPort() != -1) headerValue += ":" + host.getPort(); } else if (headerName.equalsIgnoreCase(org.apache.http.cookie.SM.COOKIE)) { headerValue = getRealCookie(headerValue); }/* ww w . ja va 2 s . co m*/ proxyRequest.addHeader(headerName, headerValue); } } }