- Timestamp:
- 2012-05-30T15:34:25+12:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/ClientSideServlet.java
r23789 r25717 1 1 package org.greenstone.gsdl3; 2 2 3 import org.apache.log4j.*; // Actually add logging? 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 4 12 import org.apache.commons.lang3.StringUtils; 13 import org.greenstone.gsdl3.util.Dictionary; 5 14 import 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;11 15 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(); 16 public class ClientSideServlet extends BaseGreenstoneServlet 17 { 18 public void init(ServletConfig config) throws ServletException 19 { 20 super.init(config); 21 } 22 22 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 26 31 String[] parts = StringUtils.split(query_string, "&"); 27 32 28 33 String[] keys = null; // Array of keys to look up 29 34 String lang = ""; 30 String interface_name = ""; 31 String command = ""; 35 String interface_name = ""; 36 String command = ""; 32 37 String params = ""; 33 34 for(String part : parts) { 35 38 39 for (String part : parts) 40 { 41 36 42 String[] nameval = StringUtils.split(part, '='); 37 43 38 if(nameval.length != 2) { 44 if (nameval.length != 2) 45 { 39 46 displayParamError(response); 40 47 return; 41 } 42 48 } 49 43 50 String name = nameval[0]; 44 51 String value = nameval[1]; 45 46 if (name.equals("k"))52 53 if (name.equals("k")) 47 54 keys = StringUtils.split(value, ","); 48 else if (name.equals("l"))55 else if (name.equals("l")) 49 56 lang = value; 50 else if (name.equals("i"))57 else if (name.equals("i")) 51 58 interface_name = value; 52 else if (name.equals("c"))59 else if (name.equals("c")) 53 60 // Alternative commands 54 61 command = value; 55 else if (name.equals("p"))62 else if (name.equals("p")) 56 63 params = value; 57 64 } 58 59 if(keys == null && command.equals("") && params.equals("")) { 65 66 if (keys == null && command.equals("") && params.equals("")) 67 { 60 68 // Not satisfiable 61 69 displayParamError(response); 62 return; 70 return; 63 71 } 64 72 65 73 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 69 78 // 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 { 74 84 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 { 76 88 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 89 } 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 { 82 97 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 modifications98 if(key.equals("null")) {99 bundle += " <item key=\"" + key + "\" value=\"\" />\r\n";100 continue;101 98 } 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 { 111 107 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"; 115 149 } 116 117 if (result == null) { // not found 118 result = "_" + original_key + "_"; 119 } 150 } 120 151 121 bundle += " <item key=\"" + encode(original_key) + "\" value=\"" + encode(result) + "\" />\r\n";122 } }123 124 152 bundle += "</textBundle>"; 125 126 response.setContentType("text/xml"); 153 154 response.setContentType("text/xml"); 127 155 128 156 w.print(bundle); 129 157 w.close(); 158 } 159 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 } 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 130 183 } 131 132 private String encode(String s) {133 s = StringUtils.replace(s, "&", "&");134 s = StringUtils.replace(s, "<", "<");135 s = StringUtils.replace(s, ">", ">");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.