Changeset 25257


Ignore:
Timestamp:
2012-03-23T14:11:02+13:00 (12 years ago)
Author:
sjm84
Message:

Various changes to support the new URLFilter and image captcha functionality

File:
1 edited

Legend:

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

    r25129 r25257  
    33import org.greenstone.gsdl3.comms.*;
    44import org.greenstone.gsdl3.core.*;
     5import org.greenstone.gsdl3.service.Authentication;
    56import org.greenstone.gsdl3.util.*;
    67import org.greenstone.gsdl3.action.PageAction; // used to get the default action
    7 import org.greenstone.util.GlobalProperties;
    88import org.w3c.dom.Document;
    99import org.w3c.dom.Element;
     
    1414import javax.servlet.http.*;
    1515
    16 import java.util.Collection;
    1716import java.util.Enumeration;
    1817import java.util.ArrayList;
     
    2120import java.util.List;
    2221import java.util.Map;
    23 import java.util.Set;
    24 import java.io.File;
    2522import java.lang.reflect.Type;
    26 import java.nio.channels.FileChannel;
    2723import java.util.Hashtable;
    2824import org.apache.log4j.*;
     
    3228
    3329// Apache Commons
    34 import org.apache.commons.fileupload.FileItem;
    35 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    36 import org.apache.commons.fileupload.servlet.ServletFileUpload;
    3730import org.apache.commons.lang3.*;
    3831
     
    277270        this.recept.configure();
    278271
     272        //Allow the message router and the document to be accessed from anywhere in this servlet context
     273        this.getServletContext().setAttribute("GSRouter", this.recept.getMessageRouter());
     274        this.getServletContext().setAttribute("GSDocument", this.doc);
    279275    }
    280276
     
    526522
    527523            }
    528 
    529524        }
    530525
     
    546541            if (!name.equals(GSXML.USER_SESSION_CACHE_ATT) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT))
    547542            {
    548 
    549543                session.removeAttribute(name);
    550544            }
     
    606600            xml_request.appendChild(xml_param_list);
    607601
    608             Enumeration params = request.getParameterNames();
    609             while (params.hasMoreElements())
    610             {
    611                 String name = (String) params.nextElement();
     602            for (String name : queryMap.keySet())
     603            {
    612604                if (!name.equals(GSParams.ACTION) && !name.equals(GSParams.SUBACTION) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSParams.OUTPUT))
    613605                {// we have already dealt with these
     
    650642
    651643            // put in all the params from the session cache
    652             params = session.getAttributeNames();
     644            Enumeration params = session.getAttributeNames();
    653645            while (params.hasMoreElements())
    654646            {
     
    701693            }
    702694        }
    703 
     695       
     696        String requestedURL = request.getRequestURL().toString();
     697        String baseURL = requestedURL.substring(0, requestedURL.indexOf(this.getServletName()));
     698        xml_request.setAttribute("baseURL", baseURL);
     699        xml_request.setAttribute("remoteAddress", request.getRemoteAddr());
     700       
     701        if(!runSecurityChecks(request, xml_request, userContext, out, baseURL, collection, document))
     702        {
     703            return;
     704        }
     705
     706        Node xml_result = this.recept.process(xml_message);
     707        encodeURLs(xml_result, response);
     708        out.println(this.converter.getPrettyString(xml_result));
     709
     710        displaySize(session_ids_table);
     711
     712    } //end of doGet(HttpServletRequest, HttpServletResponse)
     713
     714    private boolean runSecurityChecks(HttpServletRequest request, Element xml_request, UserContext userContext, PrintWriter out, String baseURL, String collection, String document) throws ServletException
     715    {
    704716        //Check if we need to login or logout
    705717        Map<String, String[]> params = request.getParameterMap();
     
    719731                request.logout();
    720732            }
    721            
     733
    722734            try
    723735            {
     736                password[0] = Authentication.hashPassword(password[0]);
    724737                request.login(username[0], password[0]);
    725738            }
    726             catch(Exception ex)
     739            catch (Exception ex)
    727740            {
    728741                //The user entered in either the wrong username or the wrong password
     
    732745                loginPageRequest.setAttribute(GSXML.SUBACTION_ATT, "login");
    733746                loginPageRequest.setAttribute(GSXML.OUTPUT_ATT, "html");
     747                loginPageRequest.setAttribute(GSXML.BASE_URL, baseURL);
    734748                loginPageMessage.appendChild(loginPageRequest);
    735749
     
    744758                Element urlParam = this.doc.createElement(GSXML.PARAM_ELEM);
    745759                urlParam.setAttribute(GSXML.NAME_ATT, "redirectURL");
    746                 urlParam.setAttribute(GSXML.VALUE_ATT, this.getServletName() + "?" + request.getQueryString().replace("&", "&amp;"));
     760                String queryString = "";
     761                if(request.getQueryString() != null)
     762                {
     763                    queryString = "?" + request.getQueryString().replace("&", "&amp;");
     764                }
     765                urlParam.setAttribute(GSXML.VALUE_ATT, this.getServletName() + queryString);
    747766                paramList.appendChild(urlParam);
    748767
     
    750769                out.println(this.converter.getPrettyString(loginPageResponse));
    751770
    752                 return;
     771                return false;
    753772            }
    754773        }
     
    757776        if (request.getAuthType() != null)
    758777        {
    759             Element userInformation = this.doc.createElement("userInformation");
    760             xml_request.appendChild(userInformation);
     778            Element userInformation = this.doc.createElement(GSXML.USER_INFORMATION_ELEM);
    761779            userInformation.setAttribute("username", request.getUserPrincipal().getName());
    762780
     
    769787
    770788            Element param = this.doc.createElement(GSXML.PARAM_ELEM);
    771             param.setAttribute(GSXML.NAME_ATT, "username");
     789            param.setAttribute(GSXML.NAME_ATT, GSXML.USERNAME_ATT);
    772790            param.setAttribute(GSXML.VALUE_ATT, request.getUserPrincipal().getName());
    773791            paramList.appendChild(param);
     
    779797                logger.error("Can't get the groups for user " + request.getUserPrincipal().getName());
    780798            }
    781 
    782             HashMap responseParams = GSXML.extractParams(responseParamList, true);
    783             String groups = (String) responseParams.get("groups");
    784 
    785             userInformation.setAttribute("groups", groups);
     799            else
     800            {
     801                HashMap responseParams = GSXML.extractParams(responseParamList, true);
     802                String groups = (String) responseParams.get(GSXML.GROUPS_ATT);
     803
     804                userInformation.setAttribute(GSXML.GROUPS_ATT, groups);
     805                xml_request.appendChild(userInformation);
     806            }
    786807        }
    787808
     
    795816            if (document != null && !document.equals(""))
    796817            {
    797                 securityRequest.setAttribute("oid", document);
     818                securityRequest.setAttribute(GSXML.NODE_OID, document);
    798819            }
    799820
     
    822843                    loginPageRequest.setAttribute(GSXML.SUBACTION_ATT, "login");
    823844                    loginPageRequest.setAttribute(GSXML.OUTPUT_ATT, "html");
     845                    loginPageRequest.setAttribute(GSXML.BASE_URL, baseURL);
    824846                    loginPageMessage.appendChild(loginPageRequest);
    825847
     
    847869                    out.println(this.converter.getPrettyString(loginPageResponse));
    848870
    849                     return;
    850                 }
    851             }
    852         }
    853 
    854         Node xml_result = this.recept.process(xml_message);
    855         encodeURLs(xml_result, response);
    856         out.println(this.converter.getPrettyString(xml_result));
    857 
    858         displaySize(session_ids_table);
    859 
    860     } //end of doGet(HttpServletRequest, HttpServletResponse)
    861 
     871                    return false;
     872                }
     873            }
     874        }
     875        return true;
     876    }
     877   
    862878    //a debugging method
    863879    private void displaySize(Hashtable table)
Note: See TracChangeset for help on using the changeset viewer.