DesktopRequest.java :  » Portal » Open-Portal » com » sun » portal » desktop » Java Open Source

Java Open Source » Portal » Open Portal 
Open Portal » com » sun » portal » desktop » DesktopRequest.java
package com.sun.portal.desktop;

import java.util.*;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.text.*;
import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.sun.portal.desktop.context.DesktopAppContext;
import com.sun.portal.desktop.util.I18n;
import com.sun.portal.desktop.util.NSStringBuffer;
import com.sun.portal.desktop.util.PIParser;
import com.sun.portal.log.common.PortalLogger;


/**
 * A DesktopRequest encapsulates the HttpServletRequest that is passed
 * to the provider, hiding and redefining those methods that are
 * inappropriate to be called by a provider.
 */

public class DesktopRequest extends HttpServletRequestWrapper {
    public static final String PREFIX = "dt.";
    public static final String IS_PORTLET_REQUEST = PREFIX + "isPortletRequest";
    
    private Hashtable desktopParameterTable = null;
    private Hashtable portletParameterTable = null;
    private String charset = null;
    private boolean isReadInputStream = false;
    private DesktopAppContext dac = null;
    
    // Create a logger for this class
    private static Logger debugLogger = PortalLogger.getLogger(DesktopRequest.class);
         
    public DesktopRequest(HttpServletRequest request, DesktopAppContext dac, boolean readFromParameters) {    
      super(request);
        this.dac = dac;
        parseParameters(readFromParameters);
    }
    
    private boolean isPortletRequest() {
        return Boolean.valueOf(super.getParameter(IS_PORTLET_REQUEST)).booleanValue();
    }
    
    protected void parseParameters(boolean readFromParameters) {
        Hashtable parameterTable = null;
        
        if (readFromParameters) {
            parameterTable = new Hashtable();
            for (Enumeration e = super.getParameterNames(); e.hasMoreElements(); ) {
                String name = (String)e.nextElement();
                String[] val = new String[1];
                val[0] = super.getParameter(name);
                parameterTable.put(name, val);
            }
        } else  if (getMethod().equalsIgnoreCase("GET") || (getContentType() == null)
        || (getContentType().indexOf("application/x-www-form-urlencoded") == -1)||(getMethod().equalsIgnoreCase("POST"))) {  
    parameterTable = new Hashtable();
    Enumeration enumReq = super.getParameterNames();
                while (enumReq.hasMoreElements()) {
                       String key = (String) enumReq.nextElement();
                       String[] val = (String[]) super.getParameterValues(key);
                       parameterTable.put(key, val);
                }   
  }      
        // read from PathInfo
        String pathInfo = getPathInfoValue();
        
        if ( pathInfo != null && pathInfo.length() != 0 ) {
            Map piMap = PIParser.parse(pathInfo);
            
            Map dtMap = (Map)piMap.get(PIParser.DESKTOP_ARGS);
            
            // merge PathInfoMap and QueryString Params.
            // When there is a Param with the same name
            // query String Param takes the precedence.
            if ( dtMap != null && !dtMap.isEmpty() ) {
                Map arrayDTMap = getArrayDTMap(dtMap);
                parameterTable = mergeParams(parameterTable, arrayDTMap);
            }
        }
        if (parameterTable == null) {
            //
            // even if parsing failed, make sure
            // to avoid NPEs
            //
            parameterTable = new Hashtable();
        }
        
        getDesktopParameterTable(parameterTable);
        getPortletParameterTable(parameterTable);
    }
    
    private  void getDesktopParameterTable(Hashtable table) {
        desktopParameterTable = new Hashtable();
        for (Iterator i = table.keySet().iterator(); i.hasNext(); ) {
            String key = (String)i.next();
            String[] val = (String[])table.get(key);
            
            if (key.startsWith(PREFIX)) {
                //
                // put the param in under both the prefixed and un-prefixed
                // value. this allows client code to use the same constants
                // for constructing URLs + fetching parameters, but
                // makes it unecessary for all code to be aware of the prefix
                //
                desktopParameterTable.put(key, val);
                // remove prefix
                key = key.substring(PREFIX.length());
                desktopParameterTable.put(key, val);
            } else if (!isPortletRequest()) {
                desktopParameterTable.put(key, val);
            }
        }
    }
    
    private void getPortletParameterTable(Map table) {
        if (!isPortletRequest()) {
            portletParameterTable = new Hashtable();
            return;
        }
        
        portletParameterTable = new Hashtable();
        for (Iterator i = table.keySet().iterator(); i.hasNext(); ) {
            String key = (String)i.next();
            if (!key.startsWith(PREFIX)) {
                String[] val = (String[])table.get(key);
                portletParameterTable.put(key, val);
            }
        }
    }
    
    private Map getArrayDTMap(Map dtMap) {
        Map arrayDTMap = new HashMap();
        Set entry = dtMap.entrySet();
        Iterator i = entry.iterator();
        while (i.hasNext()) {
            Map.Entry me = (Map.Entry)i.next();
            String Key = (String)me.getKey();
            if ((Key == null) || (Key.length() == 0)) {
                continue;
            }
            String value = (String)me.getValue();
            if ((value == null) || (value.length() == 0)) {
                continue;
            }
            String[] newval = new String[1];
            newval[0] = value;
            arrayDTMap.put(Key, newval);
        }
        return arrayDTMap;
    }
    
    private String getPathInfoValue() {
        String pi = getPathInfo();
        // bug in web container. returns the servlet path for req.getPathInfo() call for relative urls.
        if ( pi != null && pi.equals(getServletPath())) {
            pi = null;
        }
        return pi;
    }
    
    private Hashtable mergeParams(Map target, Map source) {
        Hashtable result = null;
        if ( target != null ) {
            result = new Hashtable(target);
        }
        
        // merge values from source map to target map. For keys that exist
        // in both maps, values from the target map are placed before values
        //  from the source map
        
        if ((target != null) && (source != null)) {
            Set srcEntry = source.entrySet();
            Iterator i = srcEntry.iterator();
            while (i.hasNext()) {
                Map.Entry me = (Map.Entry)i.next();
                String fromKey = (String)me.getKey();
                if ((fromKey == null) || (fromKey.length() == 0)) {
                    continue;
                }
                String[] fromVal = (String[])me.getValue();
                String[] toVal = (String[])target.get(fromKey);
                if ((toVal != null) && (toVal.length > 0)) {
                    if ((fromVal != null) && (fromVal.length > 0)) {
                        int len = toVal.length + fromVal.length;
                        String[] newval = new String[len];
                        System.arraycopy(toVal, 0, newval, 0, toVal.length);
                        System.arraycopy(fromVal, 0, newval, toVal.length, fromVal.length);
                        result.put(fromKey, newval);
                    }
                } else {
                    result.put(fromKey, fromVal);
                }
            }
        } else if (source != null) {
            result = new Hashtable(source);
        }
        if ( result == null ) {
            result = new Hashtable();
        }
        return result;
    }
    
    public void decodeParams(String charset) {
        this.charset = charset;
        
        desktopParameterTable = decodeParams(charset, desktopParameterTable);
        portletParameterTable = decodeParams(charset, portletParameterTable);
    }
    
    private static Hashtable decodeParams(String charset, Hashtable table) {
        Hashtable decodedTable = new Hashtable();
        if ( table != null ) {
            for (Enumeration e = table.keys(); e.hasMoreElements(); ) {
                String key = (String)e.nextElement();
                Object o = table.get(key);
                String decodedkey = I18n.decodeCharset(key, charset);
                if (o instanceof String[]) {
                    String vals[] = (String[])o;
                    for (int j = 0; j < vals.length; j++) {
                        vals[j] = I18n.decodeCharset(vals[j], charset);
                    }
                    //put it back in the hashtable
                    decodedTable.put(decodedkey, vals);
                } else {
                    String val = (String)o;
                    val = I18n.decodeCharset(val, charset);
                    decodedTable.put(decodedkey, val);
                }
            }
        }
        
        return decodedTable;
    }
    
    public DesktopPortletRequest getDesktopPortletRequest() {
        if (!isPortletRequest()) {
            throw new RuntimeException("is not a portlet request");
        }
        return new DesktopPortletRequest(this, portletParameterTable);
    }

    /* HttpServletRequest methods */
    
    public Map getParameterMap() {
        return desktopParameterTable;
    }
    
    public String getRequestedSessionId() {
        throw new UnsupportedOperationException("Use iPS session API to get session information");
    }
    
    public HttpSession getSession() {
        throw new UnsupportedOperationException("Use iPS session API to get session information");
    }
    
    public HttpSession getSession(boolean create) {
        throw new UnsupportedOperationException("Use iPS session API to get session information");
    }
    
    public boolean isRequestedSessionIdFromCookie() {
        throw new UnsupportedOperationException("Use iPS session API to get session information");
    }
    
    /**
     * @deprecated
     */
    public boolean isRequestedSessionIdFromUrl() {
        throw new UnsupportedOperationException("Use iPS session API to get session information");
    }
    
    public boolean isRequestedSessionIdFromURL() {
        throw new UnsupportedOperationException("Use iPS session API to get session information");
    }
    
    public boolean isRequestedSessionIdValid() {
        throw new UnsupportedOperationException("Use iPS session API to get session information");
    }
    
    /* ServletRequest methods */
    
    public String getCharacterEncoding() {
        if (charset == null) {
            //throw new UnsupportedOperationException("charset is not yet set");
            return super.getCharacterEncoding();
        }
        return charset;
    }
    
    public int getContentLength() {
        if (getContentType()!= null && getContentType().indexOf("application/x-www-form-urlencoded") == -1) {
            return super.getContentLength();
        }
        return -1;
    }
    
    public ServletInputStream getInputStream() throws IOException {
        if (getContentType()!= null && getContentType().indexOf("application/x-www-form-urlencoded") == -1) {
            if (!isReadInputStream) {
                synchronized(this) {
                    isReadInputStream = true;
                }
                return super.getInputStream();
            }
        }
        throw new UnsupportedOperationException("Request input is not available to providers");
    }
    
    public String getParameter(String name) {
        String values[] = (String[])desktopParameterTable.get(name);
        if (values != null && values.length >= 1) {
            return values[0];
        }
        return null;
    }
    
    public Enumeration getParameterNames() {
        return desktopParameterTable.keys();
    }
    
    public String[] getParameterValues(String name) {
        return (String[])desktopParameterTable.get(name);
    }
    
    public synchronized BufferedReader getReader() throws IOException {
        if (getContentType()!= null && getContentType().indexOf("application/x-www-form-urlencoded") == -1) {
            return super.getReader();
        }
        throw new UnsupportedOperationException("Request input is not available to providers");
    }
    
    /**
     * @deprecated As of Version 2.1 of the Java Servlet API, use ServletContext.getRealPath(String) instead.
     */
    public String getRealPath(String path) {
        throw new UnsupportedOperationException("getRealPath is deprecated");
        
    }
    
    public RequestDispatcher getRequestDispatcher(String path) {
        throw new UnsupportedOperationException("RequestDispatcher is not available to providers");
    }
    
    public void setDesktopParameter(String name, String value) {
  String[] values = new String[1];
  values[0] = value;
        desktopParameterTable.put(name, values);
  if (name.startsWith(PREFIX)) {
      // also set non-prefixed version
      desktopParameterTable.put(name.substring(PREFIX.length()), values);
  } else {
      // also set prefixed version
      desktopParameterTable.put(PREFIX + name, values);
  }
    }
    
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.