Changeset 38140 for gs3-installations
- Timestamp:
- 2023-09-15T18:06:37+12:00 (9 months ago)
- Location:
- gs3-installations/computing50/trunk/sites/computing50/ConnectProxyServlet/src/main
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-installations/computing50/trunk/sites/computing50/ConnectProxyServlet/src/main/java/org/greenstone/tabletop/ConnectProxyServlet.java
r38135 r38140 46 46 private static final long serialVersionUID = 1L; 47 47 protected static Logger logger = Logger.getLogger(ConnectProxyServlet.class.getName()); 48 49 protected static CheckExistsAction check_exists_ = null; 50 48 49 protected static String[] TableNames_ = null; 50 51 protected static CheckExistsAction check_exists_ = null; 51 52 protected static ArrayList<BaseAction> action_list_ = null; 52 53 54 protected String GSDL3SRCHOME_ = null; 55 protected String GSDL3_LIBRARY_URL_ = null; 56 57 protected HashMap<String,String> table_to_url_ = null; 58 53 59 public ConnectProxyServlet() { 54 } 55 56 protected static Map<String,List<String>> convertParamMapArrayToMapList(Map<String,String []> map_array) 57 { 58 Map<String,List<String>> map_list = new HashMap<String,List<String>>(); 59 60 for (String key: map_array.keySet()) { 61 List<String> val_list = Arrays.asList(map_array.get(key)); 62 63 map_list.put(key, val_list); 64 } 65 66 return map_list; 67 } 68 69 /* 70 // keep around, could be useful if needing to convert Map data in the other direction 71 protected static Map<String,String[]> convertParamMapListToMapArray(Map<String,List<String>> map_list) 72 { 73 Map<String,String[]> map_array = new HashMap<String,String[]>(); 74 75 for (String key: map_list.keySet()) { 76 String [] val_array = (String[]) map_list.get(key).toArray(); 77 map_array.put(key, val_array ); 78 } 79 80 return map_array; 81 } 82 */ 83 84 85 /** 86 * @see Servlet#init(ServletConfig) 87 */ 88 public void init(ServletConfig config) throws ServletException { 89 super.init(config); 90 91 92 String mongo_host = config.getInitParameter("mongodbHost"); 93 94 ServletContext context = getServletContext(); 95 96 if (check_exists_ == null) { 97 check_exists_ = new CheckExistsAction(context,config); 98 } 99 100 101 if (action_list_ == null) { 102 action_list_ = new ArrayList<BaseAction>(); 103 action_list_.add(check_exists_); 104 } 105 } 106 107 108 protected void displayUsage(HttpServletResponse http_servlet_response) 109 { 110 try { 111 PrintWriter pw = http_servlet_response.getWriter(); 112 113 pw.append("Usage:\n"); 114 115 for (BaseAction action: action_list_) { 116 117 pw.append(" action=" + action.getHandle() + "\n"); 118 String[] mess = action.getDescription(); 119 for (String sm: mess) { 120 pw.append(" " + sm + "\n"); 121 } 122 pw.append("\n"); 123 } 124 pw.close(); 125 } 126 catch (Exception e) { 127 e.printStackTrace(); 128 } 129 130 } 131 132 public String escapeHTML(String s) 133 { 134 StringBuilder out = new StringBuilder(Math.max(16, s.length())); 135 for (int i = 0; i < s.length(); i++) { 136 char c = s.charAt(i); 137 if (c == '"' || c == '<' || c == '>' || c == '&') { 138 out.append("&#"); 139 out.append((int) c); 140 out.append(';'); 141 } else { 142 out.append(c); 143 } 144 } 145 return out.toString(); 146 } 147 148 protected String convertGS3UrlToFile(String gs3_url) 149 { 150 String gs3_file = gs3_url 151 .replace("/greenstone3/library","C:/Table/Projects/greenstone3-svn/web") 152 .replaceAll("/","\\\\"); 153 154 return gs3_file; 155 } 156 157 158 protected String convertGS3Urls(String xml_text) 159 { 160 Pattern gsdlurl_pattern = Pattern.compile("(<GS3URL>)(.*?)(</GS3URL>)"); 161 Matcher gsdlurl_matcher = gsdlurl_pattern.matcher(xml_text); 162 163 StringBuilder mapped_output_builder = new StringBuilder(); 164 165 int last_index = 0; 166 167 while (gsdlurl_matcher.find()) { 168 mapped_output_builder.append(xml_text, last_index, gsdlurl_matcher.start()); 169 mapped_output_builder.append(convertGS3UrlToFile(gsdlurl_matcher.group(2))); 170 171 last_index = gsdlurl_matcher.end(); 172 } 173 if (last_index < xml_text.length()) { 174 mapped_output_builder.append(xml_text, last_index, xml_text.length()); 175 } 176 177 String mapped_output = mapped_output_builder.toString(); 178 179 return mapped_output; 180 } 181 182 183 184 protected void doTblPeople(HttpServletRequest request, HttpServletResponse response) 185 throws MalformedURLException, IOException 186 { 187 URL dl_url = new URL("http://localhost:8383/greenstone3/library/collection/computing-50-tabletop/browse/CL1?sa=tabletop&excerptid=classifiers"); 188 189 190 //File srcFile = new File(__dirname + "/" + export_filename); 191 //OutputStream os = response.getOutputStream(); 192 193 InputStreamReader isr = new InputStreamReader(dl_url.openStream(),StandardCharsets.UTF_8); 194 BufferedReader in = new BufferedReader(isr); 195 196 response.setHeader("Content-type", "text/xml"); 197 response.setCharacterEncoding("UTF-8"); 198 199 PrintWriter pw = response.getWriter(); 200 pw.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); 201 202 String input_line; 203 while ((input_line = in.readLine()) != null) { 204 //logger.info("input_line = '" + input_line + "'"); 205 String converted_input_line1 = convertGS3Urls(input_line); 206 String converted_input_line2 = converted_input_line1.replaceAll("<query id=\"classifiers\">","<query>"); 207 String converted_input_line3 = converted_input_line2.replaceAll("<c/>","<c></c>"); 208 String converted_input_line_final = converted_input_line3.replaceAll("<c />","<c></c>"); 209 210 if (!converted_input_line_final.matches("^\\s*$")) { 211 //logger.info("converted_line = " + converted_input_line_final); 212 pw.append(converted_input_line_final + "\n"); 213 } 214 215 } 216 } 217 218 protected void doGet(HttpServletRequest request, HttpServletResponse response) 219 throws ServletException, IOException 220 { 221 //logger.info("Handling path quest: request =" + request.toString()); 222 223 Map<String, String[]> param_map_array = request.getParameterMap(); 224 225 String[] query_args = param_map_array.get("query"); 226 227 if ((query_args != null) && (query_args.length>0)) { 228 String query_arg = query_args[query_args.length-1]; 229 230 String[] export_table_names = new String[] { 60 61 if (TableNames_ == null) { 62 TableNames_ = new String[] { 231 63 "Tbl_Acknowledgements", 232 64 "Tbl_CoolStuff", … … 242 74 "Team" 243 75 }; 244 76 } 77 } 78 79 protected static Map<String,List<String>> convertParamMapArrayToMapList(Map<String,String []> map_array) 80 { 81 Map<String,List<String>> map_list = new HashMap<String,List<String>>(); 82 83 for (String key: map_array.keySet()) { 84 List<String> val_list = Arrays.asList(map_array.get(key)); 85 86 map_list.put(key, val_list); 87 } 88 89 return map_list; 90 } 91 92 /* 93 // keep around, could be useful if needing to convert Map data in the other direction 94 protected static Map<String,String[]> convertParamMapListToMapArray(Map<String,List<String>> map_list) 95 { 96 Map<String,String[]> map_array = new HashMap<String,String[]>(); 97 98 for (String key: map_list.keySet()) { 99 String [] val_array = (String[]) map_list.get(key).toArray(); 100 map_array.put(key, val_array ); 101 } 102 103 return map_array; 104 } 105 */ 106 107 108 /** 109 * @see Servlet#init(ServletConfig) 110 */ 111 public void init(ServletConfig config) throws ServletException { 112 super.init(config); 113 114 GSDL3SRCHOME_ = config.getInitParameter("GSDL3SRCHOME"); 115 GSDL3_LIBRARY_URL_ = config.getInitParameter("GSDL3_LIBRARY_URL"); 116 117 table_to_url_ = new HashMap<String,String>(); 118 for (String table_name: TableNames_) { 119 String table_url = config.getInitParameter(table_name); 120 if ((table_url != null) && (!table_url.equals(""))) { 121 table_to_url_.put(table_name,table_url); 122 } 123 } 124 125 126 ServletContext context = getServletContext(); 127 128 if (check_exists_ == null) { 129 check_exists_ = new CheckExistsAction(context,config); 130 } 131 132 133 if (action_list_ == null) { 134 action_list_ = new ArrayList<BaseAction>(); 135 action_list_.add(check_exists_); 136 } 137 } 138 139 140 protected void displayUsage(HttpServletResponse http_servlet_response) 141 { 142 try { 143 PrintWriter pw = http_servlet_response.getWriter(); 144 145 pw.append("Usage:\n"); 146 147 for (BaseAction action: action_list_) { 148 149 pw.append(" action=" + action.getHandle() + "\n"); 150 String[] mess = action.getDescription(); 151 for (String sm: mess) { 152 pw.append(" " + sm + "\n"); 153 } 154 pw.append("\n"); 155 } 156 pw.close(); 157 } 158 catch (Exception e) { 159 e.printStackTrace(); 160 } 161 162 } 163 164 public String escapeHTML(String s) 165 { 166 StringBuilder out = new StringBuilder(Math.max(16, s.length())); 167 for (int i = 0; i < s.length(); i++) { 168 char c = s.charAt(i); 169 if (c == '"' || c == '<' || c == '>' || c == '&') { 170 out.append("&#"); 171 out.append((int) c); 172 out.append(';'); 173 } else { 174 out.append(c); 175 } 176 } 177 return out.toString(); 178 } 179 180 protected String convertGS3UrlToFile(String gs3_url) 181 { 182 String gs3_file = gs3_url 183 .replace("/greenstone3/library",GSDL3SRCHOME_+"/web") 184 .replaceAll("/","\\\\"); 185 186 return gs3_file; 187 } 188 189 190 protected String convertGS3Urls(String xml_text) 191 { 192 Pattern gsdlurl_pattern = Pattern.compile("(<GS3URL>)(.*?)(</GS3URL>)"); 193 Matcher gsdlurl_matcher = gsdlurl_pattern.matcher(xml_text); 194 195 StringBuilder mapped_output_builder = new StringBuilder(); 196 197 int last_index = 0; 198 199 while (gsdlurl_matcher.find()) { 200 mapped_output_builder.append(xml_text, last_index, gsdlurl_matcher.start()); 201 mapped_output_builder.append(convertGS3UrlToFile(gsdlurl_matcher.group(2))); 202 203 last_index = gsdlurl_matcher.end(); 204 } 205 if (last_index < xml_text.length()) { 206 mapped_output_builder.append(xml_text, last_index, xml_text.length()); 207 } 208 209 String mapped_output = mapped_output_builder.toString(); 210 211 return mapped_output; 212 } 213 214 215 protected void doTableOutput(HttpServletRequest request, HttpServletResponse response, String table_name ) 216 throws MalformedURLException, IOException 217 { 218 String table_url = table_to_url_.get(table_name); 219 URL dl_url = new URL(GSDL3_LIBRARY_URL_ + table_url); 220 221 InputStreamReader isr = new InputStreamReader(dl_url.openStream(),StandardCharsets.UTF_8); 222 BufferedReader in = new BufferedReader(isr); 223 224 response.setHeader("Content-type", "text/xml"); 225 response.setCharacterEncoding("UTF-8"); 226 227 PrintWriter pw = response.getWriter(); 228 pw.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); 229 230 String input_line; 231 while ((input_line = in.readLine()) != null) { 232 //logger.info("input_line = '" + input_line + "'"); 233 String converted_input_line1 = convertGS3Urls(input_line); 234 String converted_input_line2 = converted_input_line1.replaceAll("<query id=\"classifiers\">","<query>"); 235 String converted_input_line3 = converted_input_line2.replaceAll("<c/>","<c></c>"); 236 String converted_input_line_final = converted_input_line3.replaceAll("<c />","<c></c>"); 237 238 if (!converted_input_line_final.matches("^\\s*$")) { 239 //logger.info("converted_line = " + converted_input_line_final); 240 pw.append(converted_input_line_final + "\n"); 241 } 242 243 } 244 } 245 246 247 protected void doTblPeopleDEPRECATED(HttpServletRequest request, HttpServletResponse response) 248 throws MalformedURLException, IOException 249 { 250 // http://localhost:8383/greenstone3/library/collection/computing-50-tabletop/browse/CL1?sa=tabletop&excerptid=classifiers"); 251 252 String Tbl_People_url = table_to_url_.get("Tbl_People"); 253 URL dl_url = new URL(GSDL3_LIBRARY_URL_ + Tbl_People_url); 254 255 InputStreamReader isr = new InputStreamReader(dl_url.openStream(),StandardCharsets.UTF_8); 256 BufferedReader in = new BufferedReader(isr); 257 258 response.setHeader("Content-type", "text/xml"); 259 response.setCharacterEncoding("UTF-8"); 260 261 PrintWriter pw = response.getWriter(); 262 pw.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); 263 264 String input_line; 265 while ((input_line = in.readLine()) != null) { 266 //logger.info("input_line = '" + input_line + "'"); 267 String converted_input_line1 = convertGS3Urls(input_line); 268 String converted_input_line2 = converted_input_line1.replaceAll("<query id=\"classifiers\">","<query>"); 269 String converted_input_line3 = converted_input_line2.replaceAll("<c/>","<c></c>"); 270 String converted_input_line_final = converted_input_line3.replaceAll("<c />","<c></c>"); 271 272 if (!converted_input_line_final.matches("^\\s*$")) { 273 //logger.info("converted_line = " + converted_input_line_final); 274 pw.append(converted_input_line_final + "\n"); 275 } 276 277 } 278 } 279 280 protected void doGet(HttpServletRequest request, HttpServletResponse response) 281 throws ServletException, IOException 282 { 283 //logger.info("Handling path quest: request =" + request.toString()); 284 285 Map<String, String[]> param_map_array = request.getParameterMap(); 286 287 String[] query_args = param_map_array.get("query"); 288 289 if ((query_args != null) && (query_args.length>0)) { 290 String query_arg = query_args[query_args.length-1]; 291 245 292 String export_filename = null; 246 293 247 for (String table_name: export_table_names) {294 for (String table_name: TableNames_) { 248 295 if (query_arg.indexOf(table_name)>=0) { 249 296 logger.info("ConnectProxy matching on table-name: " + table_name); … … 259 306 260 307 if (export_filename.equals("Tbl_People.xml")) { 261 doTblPeople(request,response); 262 } 308 doTableOutput(request,response,"Tbl_People"); 309 } 310 else if (export_filename.equals("Tbl_Pictures.xml")) { 311 doTableOutput(request,response,"Tbl_Pictures"); 312 } 313 //else if (export_filename.equals("Tbl_CoolStuff.xml")) { 314 // doTableOutput(request,response,"Tbl_CoolStuff"); 315 //} 263 316 else { 264 317 … … 283 336 else { 284 337 logger.log(Level.SEVERE, "Requessted URL did not match one of the predefined table names"); 285 logger.log(Level.SEVERE, Arrays.toString( export_table_names));338 logger.log(Level.SEVERE, Arrays.toString(TableNames_)); 286 339 response.sendError(HttpServletResponse.SC_NOT_FOUND); 287 340 } -
gs3-installations/computing50/trunk/sites/computing50/ConnectProxyServlet/src/main/webapp/WEB-INF/web.xml
r38135 r38140 36 36 <init-param> 37 37 <param-name>GSDL3SRCHOME</param-name> 38 <param-value>/home/greenstone3-svn</param-value> 38 <param-value>C:/Table/Projects/greenstone3-svn</param-value> 39 </init-param> 40 41 <init-param> 42 <param-name>GSDL3_LIBRARY_URL</param-name> 43 <param-value>http://localhost:8383/greenstone3/library</param-value> 39 44 </init-param> 40 45 41 46 <init-param> 42 <param-name>Tbl_ Event</param-name>43 <param-value> CL1?sa=tabletop&excerptid=classifiers</param-value>47 <param-name>Tbl_People</param-name> 48 <param-value>/collection/computing-50-tabletop/browse/CL1?sa=tabletop&excerptid=classifiers</param-value> 44 49 </init-param> 45 50 <init-param> 46 51 <param-name>Tbl_Pictures</param-name> 47 <param-value> CL2?sa=tabletop&excerptid=classifiers</param-value>52 <param-value>/collection/computing-50-tabletop/browse/CL2?sa=tabletop&excerptid=classifiers</param-value> 48 53 </init-param> 54 49 55 <init-param> 50 <param-name>Tbl_ Pictures</param-name>51 <param-value> CL2?sa=tabletop&excerptid=classifiers</param-value>52 </init-param> 56 <param-name>Tbl_CoolStuff</param-name> 57 <param-value>/collection/computing-50-tabletop/page/about?mode=tabletop&cl=CL4&excerptid=classifiers</param-value> 58 </init-param> 53 59 54 60 <!-- the follow left in for illustration purposes, workig with an Action class -->
Note:
See TracChangeset
for help on using the changeset viewer.