public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { String contentType = req.getContentType(); if (contentType != null && contentType.startsWith(FILTERABLE_CONTENT_TYPE)) req.setCharacterEncoding(encoding); chain.doFilter(req, resp); }
private void processEncrypt(final ServletRequest servletRequest ,final HttpServletRequest httpRequest,final ServletResponse servletResponse, final FilterChain filterChain)throws IOException, ServletException{ InputStream inputStream = servletRequest.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte buf[] = new byte[1024]; int letti=-1; while ((letti = inputStream.read(buf)) > 0){ byteArrayOutputStream.write(buf, 0, letti); } final BufferedRequestWrapper bufferedRequest = new BufferedRequestWrapper(httpRequest,doEncrypt(byteArrayOutputStream)); final HttpServletResponse response = (HttpServletResponse) servletResponse; final ByteArrayPrintWriter pw = new ByteArrayPrintWriter(); HttpServletResponse wrappedResp = new HttpServletResponseWrapper(response) { public PrintWriter getWriter() { return pw.getWriter(); } public ServletOutputStream getOutputStream() { return pw.getStream(); } }; filterChain.doFilter(bufferedRequest, wrappedResp); }
/** * Get a parameter from a ServletRequest. Return null if the parameter contains only white spaces. */ public static String getParameter(ServletRequest request, String name) { String s = request.getParameter(name); if (s == null) { return null; } s = s.trim(); return s.length() == 0 ? null : s; }
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { // Get print writer. PrintWriter pw = response.getWriter(); // Get enumeration of parameter names. Enumeration e = request.getParameterNames(); // Display parameter names and values. while (e.hasMoreElements()) { String pname = (String) e.nextElement(); pw.print(pname + " = "); String pvalue = request.getParameter(pname); pw.println(pvalue); } pw.close(); }
/** * Parses HTTP parameters in an appropriate format and return back map of values to predefined * list of names. * * @param req Request. * @return Map of parsed parameters. */ @SuppressWarnings({"unchecked"}) private Map<String, Object> parameters(ServletRequest req) { Map<String, String[]> params = req.getParameterMap(); if (F.isEmpty(params)) return Collections.emptyMap(); Map<String, Object> map = U.newHashMap(params.size()); for (Map.Entry<String, String[]> entry : params.entrySet()) map.put(entry.getKey(), parameter(entry.getValue())); return map; }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // session属于http范畴,所以要将ServletRequest转换成httpServletRequest try { HttpServletRequest req = (HttpServletRequest) request; HttpSession session = req.getSession(); if (session.getAttribute("username") != null) { chain.doFilter(request, response); } else { request.getRequestDispatcher("error.jsp").forward(request, response); } } catch (RuntimeException e) { e.printStackTrace(); } }
/** * @param req Request. * @return Command. */ @Nullable GridRestCommand command(ServletRequest req) { String cmd = req.getParameter("cmd"); return cmd == null ? null : GridRestCommand.fromKey(cmd.toLowerCase()); }
/** * Creates REST request. * * @param cmd Command. * @param params Parameters. * @return REST request. * @throws GridException If creation failed. */ @Nullable private GridRestRequest createRequest( GridRestCommand cmd, Map<String, Object> params, ServletRequest req) throws GridException { GridRestRequest restReq; switch (cmd) { case CACHE_GET: case CACHE_GET_ALL: case CACHE_PUT: case CACHE_PUT_ALL: case CACHE_REMOVE: case CACHE_REMOVE_ALL: case CACHE_ADD: case CACHE_CAS: case CACHE_METRICS: case CACHE_REPLACE: case CACHE_DECREMENT: case CACHE_INCREMENT: case CACHE_APPEND: case CACHE_PREPEND: { GridRestCacheRequest restReq0 = new GridRestCacheRequest(); restReq0.cacheName((String) params.get("cacheName")); restReq0.key(params.get("key")); restReq0.value(params.get("val")); restReq0.value2(params.get("val2")); Object val1 = params.get("val1"); if (val1 != null) restReq0.value(val1); restReq0.cacheFlags(intValue("cacheFlags", params, 0)); restReq0.ttl(longValue("exp", params, null)); restReq0.initial(longValue("init", params, null)); restReq0.delta(longValue("delta", params, null)); if (cmd == CACHE_GET_ALL || cmd == CACHE_PUT_ALL || cmd == CACHE_REMOVE_ALL) { List<Object> keys = values("k", params); List<Object> vals = values("v", params); if (keys.size() < vals.size()) throw new GridException( "Number of keys must be greater or equals to number of values."); Map<Object, Object> map = U.newHashMap(keys.size()); Iterator<Object> keyIt = keys.iterator(); Iterator<Object> valIt = vals.iterator(); while (keyIt.hasNext()) map.put(keyIt.next(), valIt.hasNext() ? valIt.next() : null); restReq0.values(map); } restReq = restReq0; break; } case TOPOLOGY: case NODE: { GridRestTopologyRequest restReq0 = new GridRestTopologyRequest(); restReq0.includeMetrics(Boolean.parseBoolean((String) params.get("mtr"))); restReq0.includeAttributes(Boolean.parseBoolean((String) params.get("attr"))); restReq0.nodeIp((String) params.get("ip")); restReq0.nodeId(uuidValue("id", params)); restReq = restReq0; break; } case EXE: case RESULT: case NOOP: { GridRestTaskRequest restReq0 = new GridRestTaskRequest(); restReq0.taskId((String) params.get("id")); restReq0.taskName((String) params.get("name")); restReq0.params(values("p", params)); restReq0.async(Boolean.parseBoolean((String) params.get("async"))); restReq0.timeout(longValue("timeout", params, 0L)); restReq = restReq0; break; } case LOG: { GridRestLogRequest restReq0 = new GridRestLogRequest(); restReq0.path((String) params.get("path")); restReq0.from(intValue("from", params, -1)); restReq0.to(intValue("to", params, -1)); restReq = restReq0; break; } case VERSION: { restReq = new GridRestRequest(); break; } default: throw new GridException("Invalid command: " + cmd); } restReq.address(new InetSocketAddress(req.getRemoteAddr(), req.getRemotePort())); restReq.command(cmd); if (params.containsKey("gridgain.login") || params.containsKey("gridgain.password")) { GridSecurityCredentials cred = new GridSecurityCredentials( (String) params.get("gridgain.login"), (String) params.get("gridgain.password")); restReq.credentials(cred); } String clientId = (String) params.get("clientId"); try { if (clientId != null) restReq.clientId(UUID.fromString(clientId)); } catch (Exception ignored) { // Ignore invalid client id. Rest handler will process this logic. } String destId = (String) params.get("destId"); try { if (destId != null) restReq.destinationId(UUID.fromString(destId)); } catch (IllegalArgumentException ignored) { // Don't fail - try to execute locally. } String sesTokStr = (String) params.get("sessionToken"); try { if (sesTokStr != null) restReq.sessionToken(U.hexString2ByteArray(sesTokStr)); } catch (IllegalArgumentException ignored) { // Ignore invalid session token. } return restReq; }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (filterConfig == null) return; StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); writer.println( (new StringBuilder("Request Received at ")) .append(new Timestamp(System.currentTimeMillis())) .toString()); writer.println( (new StringBuilder(" characterEncoding=")) .append(request.getCharacterEncoding()) .toString()); writer.println( (new StringBuilder(" contentLength=")).append(request.getContentLength()).toString()); writer.println( (new StringBuilder(" contentType=")).append(request.getContentType()).toString()); writer.println( (new StringBuilder(" locale=")).append(request.getLocale()).toString()); writer.print(" locales="); Enumeration locales = request.getLocales(); boolean first = true; Locale locale; for (; locales.hasMoreElements(); writer.print(locale.toString())) { locale = (Locale) locales.nextElement(); if (first) first = false; else writer.print(", "); } writer.println(); for (Enumeration names = request.getParameterNames(); names.hasMoreElements(); writer.println()) { String name = (String) names.nextElement(); writer.print((new StringBuilder(" parameter=")).append(name).append("=").toString()); String values[] = request.getParameterValues(name); for (int i = 0; i < values.length; i++) { if (i > 0) writer.print(", "); writer.print(values[i]); } } writer.println( (new StringBuilder(" protocol=")).append(request.getProtocol()).toString()); writer.println( (new StringBuilder(" remoteAddr=")).append(request.getRemoteAddr()).toString()); writer.println( (new StringBuilder(" remoteHost=")).append(request.getRemoteHost()).toString()); writer.println( (new StringBuilder(" scheme=")).append(request.getScheme()).toString()); writer.println( (new StringBuilder(" serverName=")).append(request.getServerName()).toString()); writer.println( (new StringBuilder(" serverPort=")).append(request.getServerPort()).toString()); writer.println( (new StringBuilder(" isSecure=")).append(request.isSecure()).toString()); if (request instanceof HttpServletRequest) { writer.println("---------------------------------------------"); HttpServletRequest hrequest = (HttpServletRequest) request; writer.println( (new StringBuilder(" contextPath=")).append(hrequest.getContextPath()).toString()); Cookie cookies[] = hrequest.getCookies(); if (cookies == null) cookies = new Cookie[0]; for (int i = 0; i < cookies.length; i++) writer.println( (new StringBuilder(" cookie=")) .append(cookies[i].getName()) .append("=") .append(cookies[i].getValue()) .toString()); String name; String value; for (Enumeration names = hrequest.getHeaderNames(); names.hasMoreElements(); writer.println( (new StringBuilder(" header=")) .append(name) .append("=") .append(value) .toString())) { name = (String) names.nextElement(); value = hrequest.getHeader(name); } writer.println( (new StringBuilder(" method=")).append(hrequest.getMethod()).toString()); writer.println( (new StringBuilder(" pathInfo=")).append(hrequest.getPathInfo()).toString()); writer.println( (new StringBuilder(" queryString=")).append(hrequest.getQueryString()).toString()); writer.println( (new StringBuilder(" remoteUser="******"requestedSessionId=")) .append(hrequest.getRequestedSessionId()) .toString()); writer.println( (new StringBuilder(" requestURI=")).append(hrequest.getRequestURI()).toString()); writer.println( (new StringBuilder(" servletPath=")).append(hrequest.getServletPath()).toString()); } writer.println("============================================="); writer.flush(); filterConfig.getServletContext().log(sw.getBuffer().toString()); chain.doFilter(request, response); }