source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/ClientSideServlet.java@ 28218

Last change on this file since 28218 was 25717, checked in by sjm84, 12 years ago

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