Home » commons-chain-1.2-src » org.apache.commons » chain » web » servlet » [javadoc | source]
    1   /*
    2    * Copyright 1999-2004 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
   10    * Unless required by applicable law or agreed to in writing, software
   11    * distributed under the License is distributed on an "AS IS" BASIS,
   12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13    * See the License for the specific language governing permissions and
   14    * limitations under the License.
   15    */
   16   package org.apache.commons.chain.web.servlet;
   17   
   18   
   19   import org.apache.commons.chain.web.MockEnumeration;
   20   import org.apache.commons.chain.web.MockPrincipal;
   21   
   22   import javax.servlet.RequestDispatcher;
   23   import javax.servlet.ServletInputStream;
   24   import javax.servlet.http.Cookie;
   25   import javax.servlet.http.HttpServletRequest;
   26   import javax.servlet.http.HttpSession;
   27   import java.io.BufferedReader;
   28   import java.security.Principal;
   29   import java.util;
   30   
   31   
   32   // Mock Object for HttpServletRequest (Version 2.3)
   33   public class MockHttpServletRequest implements HttpServletRequest {
   34   
   35   
   36       public MockHttpServletRequest() {
   37           super();
   38       }
   39   
   40   
   41       public MockHttpServletRequest(HttpSession session) {
   42           super();
   43           setHttpSession(session);
   44       }
   45   
   46   
   47       public MockHttpServletRequest(String contextPath, String servletPath,
   48                                     String pathInfo, String queryString) {
   49           super();
   50           setPathElements(contextPath, servletPath, pathInfo, queryString);
   51       }
   52   
   53   
   54   
   55       public MockHttpServletRequest(String contextPath, String servletPath,
   56                                     String pathInfo, String queryString,
   57                                     HttpSession session) {
   58           super();
   59           setPathElements(contextPath, servletPath, pathInfo, queryString);
   60           setHttpSession(session);
   61       }
   62   
   63   
   64   
   65       protected HashMap attributes = new HashMap();
   66       protected String contextPath = null;
   67       protected HashMap headers = new HashMap();
   68       protected Cookie[] cookies = new Cookie[0];
   69       protected Locale locale = null;
   70       protected HashMap parameters = new HashMap();
   71       protected String pathInfo = null;
   72       protected Principal principal = null;
   73       protected String queryString = null;
   74       protected String servletPath = null;
   75       protected HttpSession session = null;
   76   
   77   
   78       // --------------------------------------------------------- Public Methods
   79   
   80   
   81       public void addHeader(String name, String value) {
   82           String values[] = (String[]) headers.get(name);
   83           if (values == null) {
   84               String results[] = new String[] { value };
   85               headers.put(name, results);
   86               return;
   87           }
   88           String results[] = new String[values.length + 1];
   89           System.arraycopy(values, 0, results, 0, values.length);
   90           results[values.length] = value;
   91           headers.put(name, results);
   92       }
   93   
   94   
   95       public void addParameter(String name, String value) {
   96           String values[] = (String[]) parameters.get(name);
   97           if (values == null) {
   98               String results[] = new String[] { value };
   99               parameters.put(name, results);
  100               return;
  101           }
  102           String results[] = new String[values.length + 1];
  103           System.arraycopy(values, 0, results, 0, values.length);
  104           results[values.length] = value;
  105           parameters.put(name, results);
  106       }
  107   
  108       public void addCookie(String name, String value) {
  109           addCookie(new Cookie(name, value));
  110       }
  111   
  112       public void addCookie(Cookie cookie) {
  113           Cookie[] newValues = new Cookie[cookies.length + 1];
  114           System.arraycopy(cookies, 0, newValues, 0, cookies.length);
  115           cookies = newValues;
  116           cookies[cookies.length - 1] = cookie;
  117       }
  118   
  119   
  120       public void setHttpSession(HttpSession session) {
  121           this.session = session;
  122       }
  123   
  124   
  125       public void setLocale(Locale locale) {
  126           this.locale = locale;
  127       }
  128   
  129   
  130       public void setPathElements(String contextPath, String servletPath,
  131                                   String pathInfo, String queryString) {
  132   
  133           this.contextPath = contextPath;
  134           this.servletPath = servletPath;
  135           this.pathInfo = pathInfo;
  136           this.queryString = queryString;
  137   
  138       }
  139   
  140   
  141       public void setUserPrincipal(Principal principal) {
  142           this.principal = principal;
  143       }
  144   
  145   
  146   
  147       // --------------------------------------------- HttpServletRequest Methods
  148   
  149   
  150       public String getAuthType() {
  151           throw new UnsupportedOperationException();
  152       }
  153   
  154   
  155       public String getContextPath() {
  156           return (contextPath);
  157       }
  158   
  159   
  160       public Cookie[] getCookies() {
  161           return cookies;
  162       }
  163   
  164   
  165       public long getDateHeader(String name) {
  166           throw new UnsupportedOperationException();
  167       }
  168   
  169   
  170       public String getHeader(String name) {
  171           String values[] = (String[]) headers.get(name);
  172           if (values != null) {
  173               return (values[0]);
  174           } else {
  175               return (null);
  176           }
  177       }
  178   
  179   
  180       public Enumeration getHeaderNames() {
  181           return (new MockEnumeration(headers.keySet().iterator()));
  182       }
  183   
  184   
  185       public Enumeration getHeaders(String name) {
  186           String headers[] = (String[]) this.headers.get(name);
  187           if (headers == null) {
  188               headers = new String[0];
  189           }
  190           List list = new ArrayList();
  191           for (int i = 0; i < headers.length; i++) {
  192               list.add(headers[i]);
  193           }
  194           return (new MockEnumeration(list.iterator()));
  195       }
  196   
  197   
  198       public int getIntHeader(String name) {
  199           throw new UnsupportedOperationException();
  200       }
  201   
  202   
  203       public String getMethod() {
  204           throw new UnsupportedOperationException();
  205       }
  206   
  207   
  208       public String getPathInfo() {
  209           return (pathInfo);
  210       }
  211   
  212   
  213       public String getPathTranslated() {
  214           throw new UnsupportedOperationException();
  215       }
  216   
  217   
  218       public String getQueryString() {
  219           return (queryString);
  220       }
  221   
  222   
  223       public String getRemoteUser() {
  224           if (principal != null) {
  225               return (principal.getName());
  226           } else {
  227               return (null);
  228           }
  229       }
  230   
  231   
  232       public String getRequestedSessionId() {
  233           throw new UnsupportedOperationException();
  234       }
  235   
  236   
  237       public String getRequestURI() {
  238           StringBuffer sb = new StringBuffer();
  239           if (contextPath != null) {
  240               sb.append(contextPath);
  241           }
  242           if (servletPath != null) {
  243               sb.append(servletPath);
  244           }
  245           if (pathInfo != null) {
  246               sb.append(pathInfo);
  247           }
  248           if (sb.length() > 0) {
  249               return (sb.toString());
  250           }
  251           throw new UnsupportedOperationException();
  252       }
  253   
  254   
  255       public StringBuffer getRequestURL() {
  256           throw new UnsupportedOperationException();
  257       }
  258   
  259   
  260       public String getServletPath() {
  261           return (servletPath);
  262       }
  263   
  264   
  265       public HttpSession getSession() {
  266           return (getSession(true));
  267       }
  268   
  269   
  270       public HttpSession getSession(boolean create) {
  271           if (create && (session == null)) {
  272               throw new UnsupportedOperationException();
  273           }
  274           return (session);
  275       }
  276   
  277   
  278       public Principal getUserPrincipal() {
  279           return (principal);
  280       }
  281   
  282   
  283       public boolean isRequestedSessionIdFromCookie() {
  284           throw new UnsupportedOperationException();
  285       }
  286   
  287   
  288       public boolean isRequestedSessionIdFromUrl() {
  289           throw new UnsupportedOperationException();
  290       }
  291   
  292   
  293       public boolean isRequestedSessionIdFromURL() {
  294           throw new UnsupportedOperationException();
  295       }
  296   
  297   
  298       public boolean isRequestedSessionIdValid() {
  299           throw new UnsupportedOperationException();
  300       }
  301   
  302   
  303       public boolean isUserInRole(String role) {
  304           if ((principal != null) && (principal instanceof MockPrincipal)) {
  305               return (((MockPrincipal) principal).isUserInRole(role));
  306           } else {
  307               return (false);
  308           }
  309       }
  310   
  311   
  312       // ------------------------------------------------- ServletRequest Methods
  313   
  314   
  315       public Object getAttribute(String name) {
  316           return (attributes.get(name));
  317       }
  318   
  319   
  320       public Enumeration getAttributeNames() {
  321           return (new MockEnumeration(attributes.keySet().iterator()));
  322       }
  323   
  324   
  325       public String getCharacterEncoding() {
  326           throw new UnsupportedOperationException();
  327       }
  328   
  329   
  330       public int getContentLength() {
  331           throw new UnsupportedOperationException();
  332       }
  333   
  334   
  335       public String getContentType() {
  336           throw new UnsupportedOperationException();
  337       }
  338   
  339   
  340       public ServletInputStream getInputStream() {
  341           throw new UnsupportedOperationException();
  342       }
  343   
  344   
  345       public Locale getLocale() {
  346           return (locale);
  347       }
  348   
  349   
  350       public Enumeration getLocales() {
  351           throw new UnsupportedOperationException();
  352       }
  353   
  354   
  355       public String getLocalAddr() {
  356           throw new UnsupportedOperationException();
  357       }
  358   
  359   
  360       public String getLocalName() {
  361       throw new UnsupportedOperationException();
  362       }
  363   
  364   
  365       public int getLocalPort() {
  366       throw new UnsupportedOperationException();
  367       }
  368   
  369   
  370       public String getParameter(String name) {
  371           String values[] = (String[]) parameters.get(name);
  372           if (values != null) {
  373               return (values[0]);
  374           } else {
  375               return (null);
  376           }
  377       }
  378   
  379   
  380       public Map getParameterMap() {
  381           return (parameters);
  382       }
  383   
  384   
  385       public Enumeration getParameterNames() {
  386           return (new MockEnumeration(parameters.keySet().iterator()));
  387       }
  388   
  389   
  390       public String[] getParameterValues(String name) {
  391           return ((String[]) parameters.get(name));
  392       }
  393   
  394   
  395       public String getProtocol() {
  396           throw new UnsupportedOperationException();
  397       }
  398   
  399   
  400       public BufferedReader getReader() {
  401           throw new UnsupportedOperationException();
  402       }
  403   
  404   
  405       public String getRealPath(String path) {
  406           throw new UnsupportedOperationException();
  407       }
  408   
  409   
  410       public String getRemoteAddr() {
  411           throw new UnsupportedOperationException();
  412       }
  413   
  414   
  415       public String getRemoteHost() {
  416           throw new UnsupportedOperationException();
  417       }
  418   
  419   
  420       public int getRemotePort() {
  421       throw new UnsupportedOperationException();
  422       }
  423   
  424   
  425       public RequestDispatcher getRequestDispatcher(String path) {
  426           throw new UnsupportedOperationException();
  427       }
  428   
  429   
  430       public String getScheme() {
  431           return ("http");
  432       }
  433   
  434   
  435       public String getServerName() {
  436           return ("localhost");
  437       }
  438   
  439   
  440       public int getServerPort() {
  441           return (8080);
  442       }
  443   
  444   
  445       public boolean isSecure() {
  446           return (false);
  447       }
  448   
  449   
  450       public void removeAttribute(String name) {
  451           attributes.remove(name);
  452       }
  453   
  454   
  455       public void setAttribute(String name, Object value) {
  456           if (value == null) {
  457               attributes.remove(name);
  458           } else {
  459               attributes.put(name, value);
  460           }
  461       }
  462   
  463   
  464       public void setCharacterEncoding(String name) {
  465           throw new UnsupportedOperationException();
  466       }
  467   
  468   
  469   }

Save This Page
Home » commons-chain-1.2-src » org.apache.commons » chain » web » servlet » [javadoc | source]