source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/greenstone3/ServletConfiguration.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 6.2 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.greenstone3;
38
39import java.awt.*;
40import java.io.*;
41import java.lang.ref.*;
42import java.net.*;
43import java.util.*;
44import javax.swing.*;
45import javax.swing.plaf.*;
46import org.greenstone.gatherer.Gatherer;
47import org.greenstone.gatherer.DebugStream;
48import org.greenstone.gatherer.util.ArrayTools;
49import org.greenstone.gatherer.util.StaticStrings;
50import org.greenstone.gatherer.util.Utility;
51import org.greenstone.gatherer.util.XMLTools;
52import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
53import org.w3c.dom.*;
54
55/** This class stores the mapping between sites and servlets available for them. The info is read in from the web.xml file.
56 * @author Katherine Don, New Zealand Digital Library, University of Waikato
57 * @version
58 */
59public class ServletConfiguration {
60
61 static public String ADD_COMMAND = "?a=s&sa=a&st=collection&sn=";
62 static public String DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
63 private String gsdl3_path = "";
64 private ArrayList sites = null;
65 private HashMap mappings = null;
66
67 public ServletConfiguration(String gsdl3_path) {
68
69 this.gsdl3_path = gsdl3_path;
70
71 if (Gatherer.isGsdlRemote){
72 if (Gatherer.remoteGreenstoneServer.downloadWebXMLFile().equals("")) {
73 System.err.println("Error: Could not download web.xml.");
74 System.exit(0);
75 }
76 }
77 //String web_xml_path = gsdl3_path + File.separator + "web" + File.separator + "WEB-INF"+ File.separator + "web.xml";
78 File web_xml;
79 if (Gatherer.isGsdlRemote){
80 web_xml = new File(gsdl3_path + "web.xml");
81 }else{
82 web_xml = new File(gsdl3_path + "WEB-INF"+ File.separator + "web.xml");
83 }
84 if (!web_xml.exists()) {
85 DebugStream.println("Error: no web.xml found at "+web_xml.toString());
86 return;
87 }
88
89 this.sites = new ArrayList();
90 if (Gatherer.isGsdlRemote){
91 String sites_on_server = Gatherer.remoteGreenstoneServer.getSiteNames();
92 if (sites_on_server.equals("")) {
93 // !! Something went wrong : could not get names of the sites
94 System.err.println("Error: Could not get names of the sites.");
95 System.exit(0);
96 }
97
98 String[] sites_arr=sites_on_server.split("-----");
99 for (int i=0; i<sites_arr.length; i++){
100 if (!(sites_arr[i].trim().equals(""))){
101 this.sites.add(sites_arr[i].trim());
102 }
103 }
104 }else{
105 // find the sites
106 File start = new File(Utility.getSitesDir(gsdl3_path));
107 File possible_sites[] = start.listFiles();
108 ArrayTools.sort(possible_sites);
109 for (int i=0; possible_sites != null && i < possible_sites.length; i++) {
110 File site_config = new File(possible_sites[i], "siteConfig.xml");
111 if (site_config.exists()) {
112 this.sites.add(possible_sites[i].getName());
113 }
114 }
115 }
116
117 this.mappings = new HashMap();
118 Document web_config = XMLTools.parseXMLFile(web_xml.getAbsolutePath(), false);
119
120 NodeList servlet_mappings = web_config.getElementsByTagName("servlet-mapping");
121 // make a little map class
122 HashMap url_mappings = new HashMap();
123 for (int i=0; i<servlet_mappings.getLength(); i++) {
124 Element map = (Element)servlet_mappings.item(i);
125 String name = XMLTools.getValue(XMLTools.getNodeFromNamed(map, "servlet-name"));
126 String pattern = XMLTools.getValue(XMLTools.getNodeFromNamed(map, "url-pattern"));
127 url_mappings.put(name, pattern);
128
129 }
130
131 NodeList servlets = web_config.getElementsByTagName("servlet");
132 for (int i=0; i<servlets.getLength(); i++) {
133 Element servlet = (Element)servlets.item(i);
134 String name = XMLTools.getValue(XMLTools.getNodeFromNamed(servlet, "servlet-name"));
135 String description = XMLTools.getValue(XMLTools.getNodeFromNamed(servlet, "description"));
136 String site = getServletSite(servlet);
137
138 if (site != null) {
139 ArrayList this_site = (ArrayList)mappings.get(site);
140 if (this_site == null) {
141 this_site = new ArrayList();
142 }
143 String url = (String)url_mappings.get(name);
144 if(url.endsWith("/*")) { // truncate trailing /*, e.g. /library/* becomes /library
145 url = url.substring(0, url.length()-2);
146 }
147 this_site.add(url);
148 mappings.put(site, this_site);
149 }
150
151 }
152
153 web_config = null;
154 url_mappings = null;
155 }
156
157 public ArrayList getSites() {
158 return this.sites;
159 }
160
161 public ArrayList getServletsForSite(String site) {
162 return (ArrayList)this.mappings.get(site);
163 }
164 public String getServletPath(String site) {
165
166 // for now, get the first one
167 ArrayList servlets = (ArrayList)mappings.get(site);
168 if(servlets == null) {
169 return null;
170 }
171 return (String)servlets.get(0);
172 }
173
174 private String getServletSite(Element servlet) {
175
176 NodeList params = servlet.getElementsByTagName("init-param");
177 if ( params == null || params.getLength() == 0) {
178 return null;
179 }
180 String site = null;
181 for (int i=0; i<params.getLength()&& site == null; i++) {
182 String p_name = XMLTools.getValue(XMLTools.getNodeFromNamed(params.item(i), "param-name"));
183 if (p_name.equals("site_name")) {
184 site = XMLTools.getValue(XMLTools.getNodeFromNamed(params.item(i), "param-value"));
185 }
186 }
187 return site;
188 }
189}
Note: See TracBrowser for help on using the repository browser.