001 package com.khubla.pragmatach.framework.api; 002 003 import java.io.ByteArrayOutputStream; 004 import java.io.InputStream; 005 import java.util.Map; 006 007 import javax.servlet.ServletConfig; 008 import javax.servlet.ServletContext; 009 import javax.servlet.http.HttpServletRequest; 010 import javax.servlet.http.HttpServletResponse; 011 import javax.servlet.http.HttpSession; 012 013 import org.apache.commons.io.IOUtils; 014 015 import com.khubla.pragmatach.framework.annotation.Route; 016 017 /** 018 * @author tome 019 */ 020 public class Request { 021 /** 022 * HttpServletRequest 023 */ 024 private final HttpServletRequest httpServletRequest; 025 /** 026 * method 027 */ 028 private final Route.HttpMethod method; 029 /** 030 * headers 031 */ 032 private final Headers headers; 033 /** 034 * cookies 035 */ 036 private final Cookies cookies; 037 /** 038 * creation time 039 */ 040 private final long creationTime = System.currentTimeMillis(); 041 /** 042 * servlet config 043 */ 044 private final ServletConfig servletConfig; 045 046 /** 047 * ctor 048 */ 049 public Request(HttpServletRequest httpServletRequest, 050 HttpServletResponse httpServletResponse, Route.HttpMethod method, 051 ServletConfig servletConfig) { 052 this.httpServletRequest = httpServletRequest; 053 this.servletConfig = servletConfig; 054 this.method = method; 055 headers = new Headers(httpServletRequest, httpServletResponse); 056 cookies = new Cookies(httpServletRequest, httpServletResponse); 057 } 058 059 public Cookies getCookies() { 060 return cookies; 061 } 062 063 public long getCreationTime() { 064 return creationTime; 065 } 066 067 public Headers getHeaders() { 068 return headers; 069 } 070 071 public HttpServletRequest getHttpServletRequest() { 072 return httpServletRequest; 073 } 074 075 public InputStream getInputStream() throws PragmatachException { 076 try { 077 return httpServletRequest.getInputStream(); 078 } catch (final Exception e) { 079 throw new PragmatachException("Exception in getInputStream", e); 080 } 081 } 082 083 public Route.HttpMethod getMethod() { 084 return method; 085 } 086 087 /** 088 * parameters 089 */ 090 public Map<String, String[]> getParameters() { 091 return httpServletRequest.getParameterMap(); 092 } 093 094 /** 095 * get the HTTP POST body 096 */ 097 public String getPostBody() throws PragmatachException { 098 try { 099 final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 100 IOUtils.copy(getInputStream(), baos); 101 return baos.toString("UTF-8"); 102 } catch (final Exception e) { 103 throw new PragmatachException("Exception in getRequestBody", e); 104 } 105 } 106 107 /** 108 * get the resource path, taking off the servlet context path 109 */ 110 public String getResourcePath() throws PragmatachException { 111 try { 112 final String uri = getURI(); 113 String ret = uri.substring(getHttpServletRequest().getContextPath() 114 .length()); 115 if (ret.endsWith("/")) { 116 ret = ret.substring(0, ret.length() - 1); 117 } 118 if ((null == ret) || (ret.length() == 0)) { 119 ret = "/"; 120 } 121 return ret; 122 } catch (final Exception e) { 123 throw new PragmatachException("Exception in resourcePath", e); 124 } 125 } 126 127 public ServletConfig getServletConfig() { 128 return servletConfig; 129 } 130 131 /** 132 * servlet context 133 */ 134 public ServletContext getServletContext() { 135 return httpServletRequest.getSession().getServletContext(); 136 } 137 138 /** 139 * session 140 */ 141 public HttpSession getSession() { 142 return httpServletRequest.getSession(); 143 } 144 145 /** 146 * URI 147 */ 148 public String getURI() { 149 return httpServletRequest.getRequestURI(); 150 } 151 }