Ignore:
Timestamp:
2012-05-30T15:34:25+12:00 (12 years ago)
Author:
sjm84
Message:

Added a BaseGreenstoneServlet servlet that the other servlets now inherit from so that we can be sure that GlobalProperties is properly initialised. There is also some reformatting and import cleaning

File:
1 edited

Legend:

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

    r23789 r25717  
    11package org.greenstone.gsdl3;
    22
    3 import org.apache.log4j.*; // Actually add logging?
     3import java.io.File;
     4import java.io.IOException;
     5import java.io.PrintWriter;
     6
     7import javax.servlet.ServletConfig;
     8import javax.servlet.ServletException;
     9import javax.servlet.http.HttpServletRequest;
     10import javax.servlet.http.HttpServletResponse;
     11
    412import org.apache.commons.lang3.StringUtils;
     13import org.greenstone.gsdl3.util.Dictionary;
    514import org.greenstone.gsdl3.util.XSLTUtil;
    6 import java.io.*;
    7 import javax.servlet.*;
    8 import javax.servlet.http.*;
    9 import org.greenstone.gsdl3.util.Dictionary;
    10 import java.util.Enumeration;
    1115
    12 public class ClientSideServlet extends HttpServlet {
    13    
    14 public void init(ServletConfig config) throws ServletException {
    15     super.init(config);
    16 }   
    17    
    18 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
    19    
    20    String query_string = request.getQueryString();
    21    PrintWriter w = response.getWriter();
     16public class ClientSideServlet extends BaseGreenstoneServlet
     17{
     18    public void init(ServletConfig config) throws ServletException
     19    {
     20        super.init(config);
     21    }
    2222
    23     if(query_string == null)
    24         displayParamError(response);
    25    
     23    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     24    {
     25        String query_string = request.getQueryString();
     26        PrintWriter w = response.getWriter();
     27
     28        if (query_string == null)
     29            displayParamError(response);
     30
    2631        String[] parts = StringUtils.split(query_string, "&");
    27    
     32
    2833        String[] keys = null; // Array of keys to look up
    2934        String lang = "";
    30         String interface_name = ""; 
    31         String command = "";   
     35        String interface_name = "";
     36        String command = "";
    3237        String params = "";
    33        
    34         for(String part : parts) {     
    35        
     38
     39        for (String part : parts)
     40        {
     41
    3642            String[] nameval = StringUtils.split(part, '=');
    3743
    38             if(nameval.length != 2) {
     44            if (nameval.length != 2)
     45            {
    3946                displayParamError(response);
    4047                return;
    41             }           
    42            
     48            }
     49
    4350            String name = nameval[0];
    4451            String value = nameval[1];
    45            
    46             if(name.equals("k"))
     52
     53            if (name.equals("k"))
    4754                keys = StringUtils.split(value, ",");
    48             else if(name.equals("l"))
     55            else if (name.equals("l"))
    4956                lang = value;
    50             else if(name.equals("i"))
     57            else if (name.equals("i"))
    5158                interface_name = value;
    52             else if(name.equals("c"))
     59            else if (name.equals("c"))
    5360                // Alternative commands
    5461                command = value;
    55             else if(name.equals("p"))
     62            else if (name.equals("p"))
    5663                params = value;
    5764        }
    58        
    59         if(keys == null && command.equals("") && params.equals("")) {
     65
     66        if (keys == null && command.equals("") && params.equals(""))
     67        {
    6068            // Not satisfiable
    6169            displayParamError(response);
    62             return; 
     70            return;
    6371        }
    64        
     72
    6573        String bundle = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\r\n<textBundle>\r\n";
    66        
    67         if(!command.equals("") && !params.equals("")) {
    68        
     74
     75        if (!command.equals("") && !params.equals(""))
     76        {
     77
    6978            // Alternative commands other than dictionary lookup
    70             String[] parameters = StringUtils.split(params, "|");           
    71             String result = null;       
    72            
    73             if(command.equals("getNumberedItem") && parameters.length == 2) {
     79            String[] parameters = StringUtils.split(params, "|");
     80            String result = null;
     81
     82            if (command.equals("getNumberedItem") && parameters.length == 2)
     83            {
    7484                result = XSLTUtil.getNumberedItem(parameters[0], Integer.parseInt(parameters[1]));
    75             } else if(command.equals("exists") && parameters.length == 2) {
     85            }
     86            else if (command.equals("exists") && parameters.length == 2)
     87            {
    7688                result = XSLTUtil.exists(parameters[0], parameters[1]) ? "true" : "false";
    77             } else if(command.equals("isImage") && parameters.length == 1) {
    78                 result = (XSLTUtil.isImage(parameters[0])) ? "true" : "false"; 
    7989            }
    80            
    81             if(result != null) {
     90            else if (command.equals("isImage") && parameters.length == 1)
     91            {
     92                result = (XSLTUtil.isImage(parameters[0])) ? "true" : "false";
     93            }
     94
     95            if (result != null)
     96            {
    8297                bundle += " <method name=\"" + command + "\" parameters=\"" + params + "\" return=\"" + result + "\" />\r\n";
    83             } else { displayParamError(response); return; }
    84         } else {
    85        
    86         for(String key : keys) {
    87        
    88             String original_key = key;
    89             String[] theParts = StringUtils.split(key, "|");
    90             String[] args = null;
    91            
    92             if(theParts.length > 1)
    93                 args = StringUtils.split(theParts[1], ";");
    94        
    95             key = theParts[0];
    96        
    97             // Straight from XSLTUtils.java (src), with some modifications
    98             if(key.equals("null")) {
    99                 bundle += " <item key=\"" + key + "\" value=\"\" />\r\n";
    100                 continue;
    10198            }
    102                
    103             Dictionary dict = new Dictionary("interface_"+interface_name, lang);
    104             String result = dict.get(key, args);
    105            
    106             if (result == null) {
    107                 String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
    108                 dict = new Dictionary(sep_interface_dir, lang);
    109                 result = dict.get(key, args); 
    110              }
     99            else
     100            {
     101                displayParamError(response);
     102                return;
     103            }
     104        }
     105        else
     106        {
    111107
    112             if (result == null && !interface_name.equals("default")) { // not found, try the default interface
    113                 dict = new Dictionary("interface_default", lang);
    114                 result = dict.get(key, args);
     108            for (String key : keys)
     109            {
     110
     111                String original_key = key;
     112                String[] theParts = StringUtils.split(key, "|");
     113                String[] args = null;
     114
     115                if (theParts.length > 1)
     116                    args = StringUtils.split(theParts[1], ";");
     117
     118                key = theParts[0];
     119
     120                // Straight from XSLTUtils.java (src), with some modifications
     121                if (key.equals("null"))
     122                {
     123                    bundle += " <item key=\"" + key + "\" value=\"\" />\r\n";
     124                    continue;
     125                }
     126
     127                Dictionary dict = new Dictionary("interface_" + interface_name, lang);
     128                String result = dict.get(key, args);
     129
     130                if (result == null)
     131                {
     132                    String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
     133                    dict = new Dictionary(sep_interface_dir, lang);
     134                    result = dict.get(key, args);
     135                }
     136
     137                if (result == null && !interface_name.equals("default"))
     138                { // not found, try the default interface
     139                    dict = new Dictionary("interface_default", lang);
     140                    result = dict.get(key, args);
     141                }
     142
     143                if (result == null)
     144                { // not found
     145                    result = "_" + original_key + "_";
     146                }
     147
     148                bundle += " <item key=\"" + encode(original_key) + "\" value=\"" + encode(result) + "\" />\r\n";
    115149            }
    116                
    117             if (result == null) { // not found
    118                 result = "_" + original_key + "_";
    119             }
     150        }
    120151
    121             bundle += " <item key=\"" + encode(original_key) + "\" value=\"" + encode(result) + "\" />\r\n";
    122         } }
    123        
    124152        bundle += "</textBundle>";
    125        
    126         response.setContentType("text/xml");           
     153
     154        response.setContentType("text/xml");
    127155
    128156        w.print(bundle);
    129157        w.close();
     158    }
     159
     160    private String encode(String s)
     161    {
     162        s = StringUtils.replace(s, "&", "&amp;");
     163        s = StringUtils.replace(s, "<", "&lt;");
     164        s = StringUtils.replace(s, ">", "&gt;");
     165        return s;
     166    }
     167
     168    private void displayParamError(HttpServletResponse response)
     169    {
     170
     171        try
     172        {
     173            PrintWriter w = response.getWriter();
     174            w.write("Invalid parameters supplied! Need key (k=), interface name (i=) and language (l=). If you specified a method call, it must be on the list of supported method calls, and you must specify a command (c=) and parameters (p=).");
     175            w.close();
     176        }
     177        catch (Exception ex)
     178        { /* Should log here */
     179        }
     180
     181    }
     182
    130183}
    131 
    132 private String encode(String s) {
    133     s = StringUtils.replace(s, "&", "&amp;");
    134     s = StringUtils.replace(s, "<", "&lt;");
    135     s = StringUtils.replace(s, ">", "&gt;");
    136     return s;
    137 }
    138 
    139 private void displayParamError(HttpServletResponse response) {
    140 
    141     try {
    142     PrintWriter w = response.getWriter();
    143     w.write("Invalid parameters supplied! Need key (k=), interface name (i=) and language (l=). If you specified a method call, it must be on the list of supported method calls, and you must specify a command (c=) and parameters (p=).");
    144     w.close();
    145     } catch (Exception ex) { /* Should log here */ }
    146    
    147 }
    148    
    149 }
Note: See TracChangeset for help on using the changeset viewer.