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

Last change on this file since 23789 was 23789, checked in by davidb, 13 years ago

The top level servlet for handling 'text grabbing' for AJAX calls for getting interface text.

File size: 4.4 KB
Line 
1package org.greenstone.gsdl3;
2
3import org.apache.log4j.*; // Actually add logging?
4import org.apache.commons.lang3.StringUtils;
5import org.greenstone.gsdl3.util.XSLTUtil;
6import java.io.*;
7import javax.servlet.*;
8import javax.servlet.http.*;
9import org.greenstone.gsdl3.util.Dictionary;
10import java.util.Enumeration;
11
12public class ClientSideServlet extends HttpServlet {
13
14public void init(ServletConfig config) throws ServletException {
15 super.init(config);
16}
17
18public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
19
20 String query_string = request.getQueryString();
21 PrintWriter w = response.getWriter();
22
23 if(query_string == null)
24 displayParamError(response);
25
26 String[] parts = StringUtils.split(query_string, "&");
27
28 String[] keys = null; // Array of keys to look up
29 String lang = "";
30 String interface_name = "";
31 String command = "";
32 String params = "";
33
34 for(String part : parts) {
35
36 String[] nameval = StringUtils.split(part, '=');
37
38 if(nameval.length != 2) {
39 displayParamError(response);
40 return;
41 }
42
43 String name = nameval[0];
44 String value = nameval[1];
45
46 if(name.equals("k"))
47 keys = StringUtils.split(value, ",");
48 else if(name.equals("l"))
49 lang = value;
50 else if(name.equals("i"))
51 interface_name = value;
52 else if(name.equals("c"))
53 // Alternative commands
54 command = value;
55 else if(name.equals("p"))
56 params = value;
57 }
58
59 if(keys == null && command.equals("") && params.equals("")) {
60 // Not satisfiable
61 displayParamError(response);
62 return;
63 }
64
65 String bundle = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\r\n<textBundle>\r\n";
66
67 if(!command.equals("") && !params.equals("")) {
68
69 // 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) {
74 result = XSLTUtil.getNumberedItem(parameters[0], Integer.parseInt(parameters[1]));
75 } else if(command.equals("exists") && parameters.length == 2) {
76 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";
79 }
80
81 if(result != null) {
82 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;
101 }
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 }
111
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);
115 }
116
117 if (result == null) { // not found
118 result = "_" + original_key + "_";
119 }
120
121 bundle += " <item key=\"" + encode(original_key) + "\" value=\"" + encode(result) + "\" />\r\n";
122 } }
123
124 bundle += "</textBundle>";
125
126 response.setContentType("text/xml");
127
128 w.print(bundle);
129 w.close();
130}
131
132private 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
139private 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 TracBrowser for help on using the repository browser.