[23789] | 1 | package org.greenstone.gsdl3;
|
---|
| 2 |
|
---|
[25717] | 3 | import java.io.File;
|
---|
| 4 | import java.io.IOException;
|
---|
| 5 | import java.io.PrintWriter;
|
---|
| 6 |
|
---|
| 7 | import javax.servlet.ServletConfig;
|
---|
| 8 | import javax.servlet.ServletException;
|
---|
| 9 | import javax.servlet.http.HttpServletRequest;
|
---|
| 10 | import javax.servlet.http.HttpServletResponse;
|
---|
| 11 |
|
---|
[23789] | 12 | import org.apache.commons.lang3.StringUtils;
|
---|
[25717] | 13 | import org.greenstone.gsdl3.util.Dictionary;
|
---|
[23789] | 14 | import org.greenstone.gsdl3.util.XSLTUtil;
|
---|
| 15 |
|
---|
[25717] | 16 | public class ClientSideServlet extends BaseGreenstoneServlet
|
---|
| 17 | {
|
---|
| 18 | public void init(ServletConfig config) throws ServletException
|
---|
| 19 | {
|
---|
| 20 | super.init(config);
|
---|
| 21 | }
|
---|
[23789] | 22 |
|
---|
[25717] | 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 |
|
---|
[23789] | 31 | String[] parts = StringUtils.split(query_string, "&");
|
---|
[25717] | 32 |
|
---|
[23789] | 33 | String[] keys = null; // Array of keys to look up
|
---|
| 34 | String lang = "";
|
---|
[25717] | 35 | String interface_name = "";
|
---|
| 36 | String command = "";
|
---|
[23789] | 37 | String params = "";
|
---|
[25717] | 38 |
|
---|
| 39 | for (String part : parts)
|
---|
| 40 | {
|
---|
| 41 |
|
---|
[23789] | 42 | String[] nameval = StringUtils.split(part, '=');
|
---|
| 43 |
|
---|
[25717] | 44 | if (nameval.length != 2)
|
---|
| 45 | {
|
---|
[23789] | 46 | displayParamError(response);
|
---|
| 47 | return;
|
---|
[25717] | 48 | }
|
---|
| 49 |
|
---|
[23789] | 50 | String name = nameval[0];
|
---|
| 51 | String value = nameval[1];
|
---|
[25717] | 52 |
|
---|
| 53 | if (name.equals("k"))
|
---|
[23789] | 54 | keys = StringUtils.split(value, ",");
|
---|
[25717] | 55 | else if (name.equals("l"))
|
---|
[23789] | 56 | lang = value;
|
---|
[25717] | 57 | else if (name.equals("i"))
|
---|
[23789] | 58 | interface_name = value;
|
---|
[25717] | 59 | else if (name.equals("c"))
|
---|
[23789] | 60 | // Alternative commands
|
---|
| 61 | command = value;
|
---|
[25717] | 62 | else if (name.equals("p"))
|
---|
[23789] | 63 | params = value;
|
---|
| 64 | }
|
---|
[25717] | 65 |
|
---|
| 66 | if (keys == null && command.equals("") && params.equals(""))
|
---|
| 67 | {
|
---|
[23789] | 68 | // Not satisfiable
|
---|
| 69 | displayParamError(response);
|
---|
[25717] | 70 | return;
|
---|
[23789] | 71 | }
|
---|
[25717] | 72 |
|
---|
[23789] | 73 | String bundle = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\r\n<textBundle>\r\n";
|
---|
[25717] | 74 |
|
---|
| 75 | if (!command.equals("") && !params.equals(""))
|
---|
| 76 | {
|
---|
| 77 |
|
---|
[23789] | 78 | // Alternative commands other than dictionary lookup
|
---|
[25717] | 79 | String[] parameters = StringUtils.split(params, "|");
|
---|
| 80 | String result = null;
|
---|
| 81 |
|
---|
| 82 | if (command.equals("getNumberedItem") && parameters.length == 2)
|
---|
| 83 | {
|
---|
[23789] | 84 | result = XSLTUtil.getNumberedItem(parameters[0], Integer.parseInt(parameters[1]));
|
---|
[25717] | 85 | }
|
---|
| 86 | else if (command.equals("exists") && parameters.length == 2)
|
---|
| 87 | {
|
---|
[23789] | 88 | result = XSLTUtil.exists(parameters[0], parameters[1]) ? "true" : "false";
|
---|
| 89 | }
|
---|
[25717] | 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 | {
|
---|
[23789] | 97 | bundle += " <method name=\"" + command + "\" parameters=\"" + params + "\" return=\"" + result + "\" />\r\n";
|
---|
| 98 | }
|
---|
[25717] | 99 | else
|
---|
| 100 | {
|
---|
| 101 | displayParamError(response);
|
---|
| 102 | return;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | else
|
---|
| 106 | {
|
---|
[23789] | 107 |
|
---|
[25717] | 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";
|
---|
[23789] | 149 | }
|
---|
[25717] | 150 | }
|
---|
[23789] | 151 |
|
---|
| 152 | bundle += "</textBundle>";
|
---|
| 153 |
|
---|
[25717] | 154 | response.setContentType("text/xml");
|
---|
| 155 |
|
---|
[23789] | 156 | w.print(bundle);
|
---|
| 157 | w.close();
|
---|
[25717] | 158 | }
|
---|
[23789] | 159 |
|
---|
[25717] | 160 | private String encode(String s)
|
---|
| 161 | {
|
---|
| 162 | s = StringUtils.replace(s, "&", "&");
|
---|
| 163 | s = StringUtils.replace(s, "<", "<");
|
---|
| 164 | s = StringUtils.replace(s, ">", ">");
|
---|
| 165 | return s;
|
---|
| 166 | }
|
---|
[23789] | 167 |
|
---|
[25717] | 168 | private void displayParamError(HttpServletResponse response)
|
---|
| 169 | {
|
---|
[23789] | 170 |
|
---|
[25717] | 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 |
|
---|
[23789] | 183 | }
|
---|