List of usage examples for javax.servlet.http HttpServletRequest getIntHeader
public int getIntHeader(String name);
int
. From source file:org.infoscoop.web.ProxyServlet.java
public void doProcess(HttpServletRequest request, HttpServletResponse response, int methodType) throws ServletException, IOException { int statusCode = 0; String url = request.getParameter("url"); ProxyRequest proxyRequest = null;/*from w ww. j a va 2 s. c o m*/ BufferedInputStream bis = null; BufferedOutputStream bos = null; try { String filterType = request.getParameter("filter"); proxyRequest = new ProxyRequest(url, filterType); String filterEncoding = request.getParameter("filterEncoding"); proxyRequest.setFilterEncoding(filterEncoding); proxyRequest.setLocales(request.getLocales()); proxyRequest.setPortalUid((String) request.getSession().getAttribute("Uid")); int timeout = request.getIntHeader("MSDPortal-Timeout") - 1000; proxyRequest.setTimeout((timeout > 0) ? timeout : DEFAULT_TIMEOUT); Enumeration headers = request.getHeaderNames(); while (headers.hasMoreElements()) { String headerName = (String) headers.nextElement(); proxyRequest.putRequestHeader(headerName, request.getHeader(headerName)); } //The certification for iframe String authTypeParam = request.getParameter("authType"); if (authTypeParam != null && !"".equals(authTypeParam)) { proxyRequest.putRequestHeader("authType", authTypeParam); proxyRequest.putRequestHeader("authuserid", request.getParameter("authuserid")); proxyRequest.putRequestHeader("authpassword", request.getParameter("authpassword")); } for (Enumeration names = request.getParameterNames(); names.hasMoreElements();) { String name = (String) names.nextElement(); proxyRequest.setFilterParameter(name, request.getParameter(name)); } try { String otherMethod = request.getHeader("MSDPortal-method"); if (otherMethod == null) otherMethod = request.getParameter("method"); if (methodType == METHOD_GET) { if (otherMethod != null && otherMethod.equalsIgnoreCase("delete")) { statusCode = proxyRequest.executeDelete(); } else if ("postCredential".equals(authTypeParam)) { statusCode = proxyRequest.executePost(); } else { statusCode = proxyRequest.executeGet(); } } else { if ("get".equalsIgnoreCase(otherMethod)) { statusCode = proxyRequest.executeGet(); } else { proxyRequest.setReqeustBody(request.getInputStream()); if (otherMethod == null || "post".equalsIgnoreCase(otherMethod)) { statusCode = proxyRequest.executePost(); } else if ("put".equalsIgnoreCase(otherMethod)) { statusCode = proxyRequest.executePut(); } else if ("report".equalsIgnoreCase(otherMethod)) { statusCode = proxyRequest.executeReport(); if (statusCode == 207) statusCode = 200; } } } } catch (SocketTimeoutException ex) { // When the status code was 408, Firefox did not move well. // ABecause the cords such as 10408 are converted into 500 by Apache-GlassFish cooperation, we set it in a header.Apache-GlassFish. response.setHeader(HttpStatusCode.HEADER_NAME, HttpStatusCode.MSD_SC_TIMEOUT); throw ex; } catch (ConnectTimeoutException ex) { // In the case of connection-timeout, we don't try it again. //response.setHeader(HttpStatusCode.HEADER_NAME, // HttpStatusCode.MSD_SC_TIMEOUT); throw ex; } Map<String, List<String>> responseHeaders = proxyRequest.getResponseHeaders(); if (statusCode == 401) { String wwwAuthHeader = proxyRequest.getResponseHeader("WWW-Authenticate"); if (wwwAuthHeader == null) { statusCode = 403; } else if (wwwAuthHeader.toLowerCase().startsWith("basic")) { statusCode = 200; response.setHeader("MSDPortal-AuthType", "basic"); } else if (wwwAuthHeader.toLowerCase().startsWith("ntlm") || wwwAuthHeader.toLowerCase().startsWith("negotiate")) { statusCode = 200; response.setHeader("MSDPortal-AuthType", "ntlm"); } } response.setStatus(statusCode); if (log.isInfoEnabled()) { log.info("Response-Status: " + statusCode); } StringBuffer headersSb = new StringBuffer(); for (Map.Entry<String, List<String>> entry : responseHeaders.entrySet()) { String name = entry.getKey(); if (name.equalsIgnoreCase("Transfer-Encoding") || name.equalsIgnoreCase("X-Powered-By")) { continue; } for (String value : entry.getValue()) { response.setHeader(entry.getKey(), value); headersSb.append(name + "=" + value + ", "); } } if (!response.containsHeader("Connection")) { response.setHeader("Connection", "close"); headersSb.append("Connection=close, "); } if (log.isInfoEnabled()) log.info("ResponseHeader: " + headersSb); String cacheHeader = request.getHeader("MSDPortal-Cache"); InputStream responseBody = proxyRequest.getResponseBody(); bos = new BufferedOutputStream(response.getOutputStream()); if (responseBody != null) { //bis = new BufferedInputStream( // new ByteArrayInputStream(bytes)); bis = new BufferedInputStream(responseBody); if (log.isDebugEnabled()) { /* bis.mark(10240000); BufferedReader br = new BufferedReader(new InputStreamReader(bis,"UTF-8")); StringBuffer logStr = new StringBuffer(); String out = null; while((out = br.readLine())!= null){ logStr.append(out); } log.debug(logStr); bis.reset(); */ bis = printDebug(bis); } String cacheID = null; if (cacheHeader != null && cacheHeader.equals("Cache-NoResponse")) { //Process to save the cash String uid = (String) request.getSession().getAttribute("Uid"); if (uid == null) uid = request.getHeader("MSDPortal-SessionId"); Map<String, List<String>> headerMap = proxyRequest.getResponseHeaders(); try { cacheID = CacheService.getHandle().insertCache(uid, url /*proxyRequest.getTargetURL() + "?" + request.getQueryString()*/, bis, headerMap); if (log.isInfoEnabled()) log.info("save cache : id = " + cacheID); } catch (Exception e) { log.error(e); //response.sendError(500, e.getMessage()); } } if (cacheHeader != null && cacheHeader.equals("Cache-NoResponse") && cacheID != null) { response.setHeader("MSDPortal-Cache-ID", cacheID); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Content-Length", "1"); //response.setHeader("Content-Length", "0"); //response.setHeader("Connection", "close"); bos.write(0); bos.flush(); //response.setStatus(204); } else { if (cacheHeader != null && cacheHeader.equals("No-Cache")) { response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); } if (!response.containsHeader("Content-Length") || statusCode == 500) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int c = 0; while ((c = bis.read(b)) != -1) { baos.write(b, 0, c); } byte[] data = baos.toByteArray(); response.addHeader("Content-Length", String.valueOf(data.length)); bis = new BufferedInputStream(new ByteArrayInputStream(data)); } byte[] b = new byte[1024]; int c = 0; while ((c = bis.read(b)) != -1) { bos.write(b, 0, c); } bos.flush(); } } else { if (statusCode == HttpStatus.SC_NO_CONTENT) { response.setHeader("Content-Length", "0"); } else { response.setHeader("Content-Length", "1"); bos.write(0); bos.flush(); } } long elapsedtime = new Date().getTime() - lastDeleteCachesTime; if (elapsedtime > 86400000) { log.info("Delete old public caches."); lastDeleteCachesTime = new Date().getTime(); CacheService.getHandle().deleteOldPublicCaches(); } } catch (Exception e) { log.error("Failed to get the URL. " + buildMessage(statusCode, proxyRequest, url), e); response.sendError(500, e.getMessage()); } finally { if (log.isInfoEnabled()) { log.info("Succeeded in getting the URL. " + buildMessage(statusCode, proxyRequest, url)); } if (proxyRequest != null) proxyRequest.close(); if (bis != null) bis.close(); if (bos != null) bos.close(); } }
From source file:org.infoscoop.web.WidgetConfServlet.java
private String getGadgetConfJson(HttpServletRequest request, String type, Locale locale) throws Exception { int timeout = request.getIntHeader("MSDPortal-Timeout") - 1000; WidgetConfUtil.GadgetContext context = new WidgetConfUtil.GadgetContext().setTimeout(timeout).setUrl(type); Document doc = context.getDocument(request); JSONObject jsonObj = WidgetConfUtil.gadget2JSONObject(doc.getDocumentElement(), context.getI18NConveter(locale, doc), true); jsonObj = WidgetConfUtil.gadgetJSONtoPortalGadgetJSON(jsonObj); return I18NUtil.resolve(I18NUtil.TYPE_WIDGET, jsonObj.toString(1), locale, true); }
From source file:org.thingsboard.server.controller.BaseController.java
protected String constructBaseUrl(HttpServletRequest request) { String scheme = request.getScheme(); if (request.getHeader("x-forwarded-proto") != null) { scheme = request.getHeader("x-forwarded-proto"); }//from w w w . java 2 s. c o m int serverPort = request.getServerPort(); if (request.getHeader("x-forwarded-port") != null) { try { serverPort = request.getIntHeader("x-forwarded-port"); } catch (NumberFormatException e) { } } String baseUrl = String.format("%s://%s:%d", scheme, request.getServerName(), serverPort); return baseUrl; }