Ignore:
Timestamp:
2013-06-13T13:13:46+12:00 (11 years ago)
Author:
sjm84
Message:

Various improvements and fixes mostly to do with adding depositor functionality

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java

    r27074 r27617  
    11package org.greenstone.gsdl3;
    22
     3import java.io.File;
     4import java.io.FileInputStream;
     5import java.io.FileOutputStream;
    36import java.io.IOException;
    47import java.io.PrintWriter;
    58import java.io.Serializable;
    69import java.lang.reflect.Type;
     10import java.nio.channels.FileChannel;
    711import java.util.ArrayList;
     12import java.util.Collection;
    813import java.util.Enumeration;
    914import java.util.HashMap;
     
    2126import javax.servlet.http.HttpSessionBindingEvent;
    2227import javax.servlet.http.HttpSessionBindingListener;
    23 
     28import javax.servlet.http.Part;
     29
     30import org.apache.commons.fileupload.FileItem;
     31import org.apache.commons.fileupload.disk.DiskFileItemFactory;
     32import org.apache.commons.fileupload.servlet.ServletFileUpload;
    2433import org.apache.commons.lang3.StringUtils;
    2534import org.apache.log4j.Logger;
     
    3645import org.greenstone.gsdl3.util.UserContext;
    3746import org.greenstone.gsdl3.util.XMLConverter;
     47import org.greenstone.util.GlobalProperties;
    3848import org.json.JSONObject;
    3949import org.w3c.dom.Document;
     
    169179        }
    170180
    171         HashMap<String, Comparable> config_params = new HashMap<String, Comparable>();
     181        HashMap<String, Object> config_params = new HashMap<String, Object>();
    172182
    173183        config_params.put(GSConstants.LIBRARY_NAME, library_name);
     
    315325    public class UserSessionCache implements HttpSessionBindingListener
    316326    {
    317 
    318327        String session_id = "";
    319328
     
    368377    }
    369378
    370     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     379    public void doGetOrPost(HttpServletRequest request, HttpServletResponse response, Map<String, String[]> queryMap) throws ServletException, IOException
    371380    {
    372381        logUsageInfo(request);
    373382
    374         Map<String, String[]> queryMap = request.getParameterMap();
    375383        if (queryMap != null)
    376384        {
     
    380388            String rl = null;
    381389            String el = null;
     390
    382391            while (queryIter.hasNext())
    383392            {
     
    437446        PrintWriter out = response.getWriter();
    438447
    439         String lang = request.getParameter(GSParams.LANGUAGE);
     448        String lang = getFirstParam(GSParams.LANGUAGE, queryMap);
    440449        if (lang == null || lang.equals(""))
    441450        {
     
    454463        if (request.getAuthType() != null)
    455464        {
     465            //Get the username
     466            userContext.setUsername(request.getUserPrincipal().getName());
     467
    456468            //Get the groups for the user
    457469            Element acquireGroupMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
     
    461473            Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
    462474            acquireGroupRequest.appendChild(paramList);
    463             paramList.appendChild(GSXML.createParameter(this.doc, "username", request.getUserPrincipal().getName()));
     475            paramList.appendChild(GSXML.createParameter(this.doc, GSXML.USERNAME_ATT, request.getUserPrincipal().getName()));
    464476
    465477            Element aquireGroupsResponseMessage = (Element) this.recept.process(acquireGroupMessage);
     
    478490        session.setAttribute(GSParams.LANGUAGE, lang);
    479491
    480         String output = request.getParameter(GSParams.OUTPUT);
     492        String output = getFirstParam(GSParams.OUTPUT, queryMap);
    481493        if (output == null || output.equals(""))
    482494        {
     
    517529        xml_message.appendChild(xml_request);
    518530
    519         String action = request.getParameter(GSParams.ACTION);
    520         String subaction = request.getParameter(GSParams.SUBACTION);
    521         String collection = request.getParameter(GSParams.COLLECTION);
    522         String document = request.getParameter(GSParams.DOCUMENT);
    523         String service = request.getParameter(GSParams.SERVICE);
     531        String action = getFirstParam(GSParams.ACTION, queryMap);
     532        String subaction = getFirstParam(GSParams.SUBACTION, queryMap);
     533        String collection = getFirstParam(GSParams.COLLECTION, queryMap);
     534        String document = getFirstParam(GSParams.DOCUMENT, queryMap);
     535        String service = getFirstParam(GSParams.SERVICE, queryMap);
    524536
    525537        // We clean up the cache session_ids_table if system
     
    536548            // collection param is in the sc parameter.
    537549            // don't like the fact that it is hard coded here
    538             String coll = request.getParameter(GSParams.SYSTEM_CLUSTER);
     550            String coll = getFirstParam(GSParams.SYSTEM_CLUSTER, queryMap);
    539551            if (coll != null && !coll.equals(""))
    540552            {
     
    547559                if (subaction.equals("a") || subaction.equals("d"))
    548560                {
    549                     String module_name = request.getParameter("sn");
     561                    String module_name = getFirstParam("sn", queryMap);
    550562                    if (module_name != null && !module_name.equals(""))
    551563                    {
     
    655667
    656668                    String value = "";
    657                     String[] values = request.getParameterValues(name);
     669                    String[] values = queryMap.get(name);
    658670                    value = values[0];
    659671                    if (values.length > 1)
     
    722734
    723735        //Add custom HTTP headers if requested
    724         String httpHeadersParam = request.getParameter(GSParams.HTTP_HEADER_FIELDS);
     736        String httpHeadersParam = getFirstParam(GSParams.HTTP_HEADER_FIELDS, queryMap);
    725737        if (httpHeadersParam != null && httpHeadersParam.length() > 0)
    726738        {
     
    768780        xml_request.setAttribute("fullURL", fullURL.replace("&", "&amp;"));
    769781
    770         if (!runSecurityChecks(request, xml_request, userContext, out, baseURL, collection, document))
     782        if (!runSecurityChecks(request, xml_request, userContext, out, baseURL, collection, document, queryMap))
    771783        {
    772784            return;
     
    798810
    799811        displaySize(session_ids_table);
    800 
     812    }
     813
     814    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     815    {
     816        doGetOrPost(request, response, request.getParameterMap());
    801817    } //end of doGet(HttpServletRequest, HttpServletResponse)
    802818
    803     private boolean runSecurityChecks(HttpServletRequest request, Element xml_request, UserContext userContext, PrintWriter out, String baseURL, String collection, String document) throws ServletException
     819    private boolean runSecurityChecks(HttpServletRequest request, Element xml_request, UserContext userContext, PrintWriter out, String baseURL, String collection, String document, Map<String, String[]> queryMap) throws ServletException
    804820    {
    805821        //Check if we need to login or logout
    806         Map<String, String[]> params = request.getParameterMap();
    807         String[] username = params.get("username");
    808         String[] password = params.get("password");
    809         String[] logout = params.get("logout");
     822        String username = getFirstParam("username", queryMap);
     823        String password = getFirstParam("password", queryMap);
     824        String logout = getFirstParam("logout", queryMap);
    810825
    811826        if (logout != null)
     
    822837            }
    823838
     839            //This try/catch block catches when the login request fails (e.g. The user enters an incorrect password).
    824840            try
    825841            {
    826                 password[0] = Authentication.hashPassword(password[0]);
    827                 request.login(username[0], password[0]);
     842                //Try a global login first
     843                password = Authentication.hashPassword(password);
     844                request.login(username, password);
    828845            }
    829846            catch (Exception ex)
    830847            {
    831                 //The user entered in either the wrong username or the wrong password
    832                 Element loginPageMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
    833                 Element loginPageRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", userContext);
    834                 loginPageRequest.setAttribute(GSXML.ACTION_ATT, "p");
    835                 loginPageRequest.setAttribute(GSXML.SUBACTION_ATT, "login");
    836                 loginPageRequest.setAttribute(GSXML.OUTPUT_ATT, "html");
    837                 loginPageRequest.setAttribute(GSXML.BASE_URL, baseURL);
    838                 loginPageMessage.appendChild(loginPageRequest);
    839 
    840                 Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
    841                 loginPageRequest.appendChild(paramList);
    842 
    843                 Element messageParam = this.doc.createElement(GSXML.PARAM_ELEM);
    844                 messageParam.setAttribute(GSXML.NAME_ATT, "loginMessage");
    845                 messageParam.setAttribute(GSXML.VALUE_ATT, "Either your username or password was incorrect, please try again.");
    846                 paramList.appendChild(messageParam);
    847 
    848                 Element urlParam = this.doc.createElement(GSXML.PARAM_ELEM);
    849                 urlParam.setAttribute(GSXML.NAME_ATT, "redirectURL");
    850                 String queryString = "";
    851                 if (request.getQueryString() != null)
    852                 {
    853                     queryString = "?" + request.getQueryString().replace("&", "&amp;");
    854                 }
    855                 urlParam.setAttribute(GSXML.VALUE_ATT, this.getServletName() + queryString);
    856                 paramList.appendChild(urlParam);
    857 
    858                 Node loginPageResponse = this.recept.process(loginPageMessage);
    859                 out.println(this.converter.getPrettyString(loginPageResponse));
    860 
    861                 return false;
    862             }
    863         }
    864 
    865         //If a user is logged in
    866         if (request.getAuthType() != null)
    867         {
    868             Element userInformation = this.doc.createElement(GSXML.USER_INFORMATION_ELEM);
    869             userInformation.setAttribute("username", request.getUserPrincipal().getName());
    870 
    871             Element userInfoMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
    872             Element userInfoRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_SECURITY, "GetUserInformation", userContext);
    873             userInfoMessage.appendChild(userInfoRequest);
    874 
    875             Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
    876             userInfoRequest.appendChild(paramList);
    877 
    878             Element param = this.doc.createElement(GSXML.PARAM_ELEM);
    879             param.setAttribute(GSXML.NAME_ATT, GSXML.USERNAME_ATT);
    880             param.setAttribute(GSXML.VALUE_ATT, request.getUserPrincipal().getName());
    881             paramList.appendChild(param);
    882 
    883             Element userInformationResponse = (Element) GSXML.getChildByTagName(this.recept.process(userInfoMessage), GSXML.RESPONSE_ELEM);
    884             Element responseParamList = (Element) GSXML.getChildByTagName(userInformationResponse, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
    885             if (responseParamList == null)
    886             {
    887                 logger.error("Can't get the groups for user " + request.getUserPrincipal().getName());
    888             }
    889             else
    890             {
    891                 HashMap<String, Serializable> responseParams = GSXML.extractParams(responseParamList, true);
    892                 String groups = (String) responseParams.get(GSXML.GROUPS_ATT);
    893 
    894                 userInformation.setAttribute(GSXML.GROUPS_ATT, groups);
    895                 xml_request.appendChild(userInformation);
    896             }
    897         }
    898 
    899         //If we are in a collection-related page then make sure this user is allowed to access it
    900         if (collection != null && !collection.equals(""))
    901         {
    902             //Get the security info for this collection
    903             Element securityMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
    904             Element securityRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_SECURITY, collection, userContext);
    905             securityMessage.appendChild(securityRequest);
    906             if (document != null && !document.equals(""))
    907             {
    908                 securityRequest.setAttribute(GSXML.NODE_OID, document);
    909             }
    910 
    911             Element securityResponse = (Element) GSXML.getChildByTagName(this.recept.process(securityMessage), GSXML.RESPONSE_ELEM);
    912             if (securityResponse == null)
    913             {
    914                 return false;
    915             }
    916 
    917             ArrayList<String> groups = GSXML.getGroupsFromSecurityResponse(securityResponse);
    918 
    919             //If guests are not allowed to access this page then check to see if the user is in a group that is allowed to access the page
    920             if (!groups.contains(""))
    921             {
    922                 boolean found = false;
    923                 for (String group : groups)
    924                 {
    925                     if (request.isUserInRole(group))
    926                     {
    927                         found = true;
    928                         break;
    929                     }
    930                 }
    931 
    932                 //The current user is not allowed to access the page so produce a login page
    933                 if (!found)
    934                 {
     848                try
     849                {
     850                    //If the global login fails then try a site-level login
     851                    String siteName = (String) this.recept.getConfigParams().get(GSConstants.SITE_NAME);
     852                    request.login(siteName + "-" + username, password);
     853                }
     854                catch (Exception exc)
     855                {
     856                    //The user entered in either the wrong username or the wrong password
    935857                    Element loginPageMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
    936858                    Element loginPageRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", userContext);
     
    946868                    Element messageParam = this.doc.createElement(GSXML.PARAM_ELEM);
    947869                    messageParam.setAttribute(GSXML.NAME_ATT, "loginMessage");
     870                    messageParam.setAttribute(GSXML.VALUE_ATT, "Either your username or password was incorrect, please try again.");
     871                    paramList.appendChild(messageParam);
     872
     873                    Element urlParam = this.doc.createElement(GSXML.PARAM_ELEM);
     874                    urlParam.setAttribute(GSXML.NAME_ATT, "redirectURL");
     875                    String queryString = "";
     876                    if (request.getQueryString() != null)
     877                    {
     878                        queryString = "?" + request.getQueryString().replace("&", "&amp;");
     879                    }
     880                    urlParam.setAttribute(GSXML.VALUE_ATT, this.getServletName() + queryString);
     881                    paramList.appendChild(urlParam);
     882
     883                    Node loginPageResponse = this.recept.process(loginPageMessage);
     884                    out.println(this.converter.getPrettyString(loginPageResponse));
     885
     886                    return false;
     887                }
     888            }
     889        }
     890
     891        //If a user is logged in
     892        if (request.getAuthType() != null)
     893        {
     894            Element userInformation = this.doc.createElement(GSXML.USER_INFORMATION_ELEM);
     895            userInformation.setAttribute(GSXML.USERNAME_ATT, request.getUserPrincipal().getName());
     896
     897            Element userInfoMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
     898            Element userInfoRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_SECURITY, "GetUserInformation", userContext);
     899            userInfoMessage.appendChild(userInfoRequest);
     900
     901            Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     902            userInfoRequest.appendChild(paramList);
     903
     904            Element param = this.doc.createElement(GSXML.PARAM_ELEM);
     905            param.setAttribute(GSXML.NAME_ATT, GSXML.USERNAME_ATT);
     906            param.setAttribute(GSXML.VALUE_ATT, request.getUserPrincipal().getName());
     907            paramList.appendChild(param);
     908
     909            Element userInformationResponse = (Element) GSXML.getChildByTagName(this.recept.process(userInfoMessage), GSXML.RESPONSE_ELEM);
     910            Element responseParamList = (Element) GSXML.getChildByTagName(userInformationResponse, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     911            if (responseParamList == null)
     912            {
     913                logger.error("Can't get the groups for user " + request.getUserPrincipal().getName());
     914            }
     915            else
     916            {
     917                HashMap<String, Serializable> responseParams = GSXML.extractParams(responseParamList, true);
     918                String groups = (String) responseParams.get(GSXML.GROUPS_ATT);
     919
     920                userInformation.setAttribute(GSXML.GROUPS_ATT, groups);
     921                xml_request.appendChild(userInformation);
     922            }
     923        }
     924
     925        //If we are in a collection-related page then make sure this user is allowed to access it
     926        if (collection != null && !collection.equals(""))
     927        {
     928            //Get the security info for this collection
     929            Element securityMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
     930            Element securityRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_SECURITY, collection, userContext);
     931            securityMessage.appendChild(securityRequest);
     932            if (document != null && !document.equals(""))
     933            {
     934                securityRequest.setAttribute(GSXML.NODE_OID, document);
     935            }
     936
     937            Element securityResponse = (Element) GSXML.getChildByTagName(this.recept.process(securityMessage), GSXML.RESPONSE_ELEM);
     938            if (securityResponse == null)
     939            {
     940                return false;
     941            }
     942
     943            ArrayList<String> groups = GSXML.getGroupsFromSecurityResponse(securityResponse);
     944
     945            //If guests are not allowed to access this page then check to see if the user is in a group that is allowed to access the page
     946            if (!groups.contains(""))
     947            {
     948                boolean found = false;
     949                for (String group : groups)
     950                {
     951                    if (request.isUserInRole(group))
     952                    {
     953                        found = true;
     954                        break;
     955                    }
     956                }
     957
     958                //The current user is not allowed to access the page so produce a login page
     959                if (!found)
     960                {
     961                    Element loginPageMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
     962                    Element loginPageRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", userContext);
     963                    loginPageRequest.setAttribute(GSXML.ACTION_ATT, "p");
     964                    loginPageRequest.setAttribute(GSXML.SUBACTION_ATT, "login");
     965                    loginPageRequest.setAttribute(GSXML.OUTPUT_ATT, "html");
     966                    loginPageRequest.setAttribute(GSXML.BASE_URL, baseURL);
     967                    loginPageMessage.appendChild(loginPageRequest);
     968
     969                    Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     970                    loginPageRequest.appendChild(paramList);
     971
     972                    Element messageParam = this.doc.createElement(GSXML.PARAM_ELEM);
     973                    messageParam.setAttribute(GSXML.NAME_ATT, "loginMessage");
    948974                    if (request.getAuthType() == null)
    949975                    {
     
    10201046    protected void encodeURLs(Node dataNode, HttpServletResponse response)
    10211047    {
    1022 
    10231048        if (dataNode == null)
    10241049        {
     
    10871112    }
    10881113
     1114    protected String getFirstParam(String name, Map<String, String[]> map)
     1115    {
     1116        String[] val = map.get(name);
     1117        if (val == null || val.length == 0)
     1118        {
     1119            return null;
     1120        }
     1121
     1122        return val[0];
     1123    }
     1124
    10891125    synchronized protected int getNextUserId()
    10901126    {
     
    10951131    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    10961132    {
    1097         doGet(request, response);
     1133        //Check if we need to process a file upload
     1134        if (ServletFileUpload.isMultipartContent(request))
     1135        {
     1136            DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
     1137
     1138            int sizeLimit = System.getProperties().containsKey("servlet.upload.filesize.limit") ? Integer.parseInt(System.getProperty("servlet.upload.filesize.limit")) : 100 * 1024 * 1024;
     1139
     1140            File tempDir = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp");
     1141            if (!tempDir.exists())
     1142            {
     1143                tempDir.mkdirs();
     1144            }
     1145
     1146            //We want all files to be stored on disk (hence the 0)
     1147            fileItemFactory.setSizeThreshold(0);
     1148            fileItemFactory.setRepository(tempDir);
     1149
     1150            ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
     1151            uploadHandler.setFileSizeMax(sizeLimit);
     1152
     1153            HashMap<String, String[]> queryMap = new HashMap<String, String[]>();
     1154            try
     1155            {
     1156                List items = uploadHandler.parseRequest(request);
     1157                Iterator iter = items.iterator();
     1158                while (iter.hasNext())
     1159                {
     1160                    FileItem current = (FileItem) iter.next();
     1161                    if (current.isFormField())
     1162                    {
     1163                        queryMap.put(current.getFieldName(), new String[] { current.getString() });
     1164                    }
     1165                    else if (current.getName() != null && !current.getName().equals(""))
     1166                    {
     1167                        File file = new File(tempDir, current.getName());
     1168                        current.write(file);
     1169                    }
     1170                }
     1171            }
     1172            catch (Exception e)
     1173            {
     1174                e.printStackTrace();
     1175            }
     1176
     1177            doGetOrPost(request, response, queryMap);
     1178        }
     1179        else
     1180        {
     1181            doGetOrPost(request, response, request.getParameterMap());
     1182        }
    10981183    }
    10991184}
Note: See TracChangeset for help on using the changeset viewer.