1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 | // XML classes
|
---|
6 | import org.w3c.dom.Node;
|
---|
7 | import org.w3c.dom.NodeList;
|
---|
8 | import org.w3c.dom.Document;
|
---|
9 | import org.w3c.dom.Element;
|
---|
10 |
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.io.File;
|
---|
13 |
|
---|
14 | public class PageAction extends Action {
|
---|
15 |
|
---|
16 | public static final String HOME_PAGE = "home";
|
---|
17 | public static final String ABOUT_PAGE = "about";
|
---|
18 | public static final String PREFS_PAGE = "pref";
|
---|
19 | public Element process (Element message) {
|
---|
20 |
|
---|
21 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
22 | // the page name is the subaction
|
---|
23 | String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
24 | if (page_name.equals("")) { // if no page specified, assume home page
|
---|
25 | page_name = HOME_PAGE;
|
---|
26 | }
|
---|
27 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
28 | Element response;
|
---|
29 | if (page_name.equals(HOME_PAGE)) {
|
---|
30 | response = homePage(request);
|
---|
31 | } else if (page_name.equals(ABOUT_PAGE)) {
|
---|
32 | response = aboutPage(request);
|
---|
33 | // } else if (page_name.equals(PREFS_PAGE)) {
|
---|
34 | // response = prefsPage(request);
|
---|
35 | } else { // unknown page
|
---|
36 |
|
---|
37 | System.err.println("PageAction Error: unknown page specified!");
|
---|
38 | response = unknownPage(request);
|
---|
39 | }
|
---|
40 |
|
---|
41 | result.appendChild(this.doc.importNode(response, true));
|
---|
42 | ///ystem.out.println("page action result: "+this.converter.getPrettyString(result));
|
---|
43 | return result;
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | protected Element homePage(Element request) {
|
---|
48 |
|
---|
49 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
50 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
51 | // first, get the message router info
|
---|
52 | Element coll_list_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
53 | Element coll_list_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid);
|
---|
54 | coll_list_message.appendChild(coll_list_request);
|
---|
55 | Element coll_list_response = (Element)this.mr.process(coll_list_message);
|
---|
56 | if (coll_list_response==null) {
|
---|
57 | System.err.println("PageAction Error: couldn't query the message router!");
|
---|
58 | return null;
|
---|
59 | }
|
---|
60 |
|
---|
61 | // second, get the metadata for each collection - we only want specific
|
---|
62 | // elements but for now, we'll just get it all
|
---|
63 | NodeList colls = coll_list_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
64 | // we will send all the requests in a single message
|
---|
65 | Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
66 | for (int i=0; i<colls.getLength(); i++) {
|
---|
67 | Element c = (Element)colls.item(i);
|
---|
68 | String name = c.getAttribute(GSXML.NAME_ATT);
|
---|
69 |
|
---|
70 | Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, name, lang, uid);
|
---|
71 | metadata_message.appendChild(metadata_request);
|
---|
72 | }
|
---|
73 |
|
---|
74 | Element metadata_response = (Element)this.mr.process(metadata_message);
|
---|
75 |
|
---|
76 | NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
77 | // check that have same number of responses as collections
|
---|
78 | if (colls.getLength() != coll_responses.getLength()) {
|
---|
79 | System.err.println("PageAction Error: didn't get a response for each collection - somethings gone wrong!");
|
---|
80 | // for now, dont use the metadata
|
---|
81 | } else {
|
---|
82 | for (int i=0; i<colls.getLength(); i++) {
|
---|
83 | Element c1 = (Element)colls.item(i);
|
---|
84 | Element c2 = (Element)coll_responses.item(i);
|
---|
85 | if (c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.FROM_ATT))) {
|
---|
86 | //add the collection data into the original response
|
---|
87 | GSXML.mergeElements(c1, (Element)GSXML.getChildByTagName(c2, GSXML.COLLECTION_ELEM));
|
---|
88 | } else {
|
---|
89 | System.err.println("PageAction Error: response does not correspond to request!");
|
---|
90 | }
|
---|
91 |
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | //now the full response message is in coll_list_response
|
---|
96 | // return the response element
|
---|
97 | Element response = (Element) GSXML.getChildByTagName(coll_list_response, GSXML.RESPONSE_ELEM);
|
---|
98 | return response;
|
---|
99 |
|
---|
100 | } // homePage
|
---|
101 |
|
---|
102 | protected Element aboutPage(Element request) {
|
---|
103 |
|
---|
104 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
105 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
106 | // extract the params from the cgi-request,
|
---|
107 | Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
108 | HashMap params = GSXML.extractParams(cgi_paramList, false);
|
---|
109 |
|
---|
110 | String coll_name = (String)params.get(GSParams.COLLECTION);
|
---|
111 | if (coll_name == null || coll_name.equals("")) {
|
---|
112 | System.err.println("PageAction Error: about page requested with no collection or cluster specified!");
|
---|
113 | // return an empty response
|
---|
114 | return this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
115 | }
|
---|
116 |
|
---|
117 | // get the collection or cluster description
|
---|
118 | Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
119 |
|
---|
120 | Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang, uid);
|
---|
121 | coll_about_message.appendChild(coll_about_request);
|
---|
122 |
|
---|
123 | Element coll_about_response = (Element)this.mr.process(coll_about_message);
|
---|
124 |
|
---|
125 | if (coll_about_response == null) {
|
---|
126 | return null;
|
---|
127 | }
|
---|
128 |
|
---|
129 | // second, get the info for each service - we only want display items
|
---|
130 | // but for now, we'll just get it all
|
---|
131 | NodeList services = coll_about_response.getElementsByTagName(GSXML.SERVICE_ELEM);
|
---|
132 | // we will send all the requests in a single message
|
---|
133 | Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
134 | for (int i=0; i<services.getLength(); i++) {
|
---|
135 | Element c = (Element)services.item(i);
|
---|
136 | String name = c.getAttribute(GSXML.NAME_ATT);
|
---|
137 | String address = GSPath.appendLink(coll_name, name);
|
---|
138 | Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, address, lang, uid);
|
---|
139 | info_message.appendChild(info_request);
|
---|
140 | }
|
---|
141 |
|
---|
142 | Element info_response = (Element)this.mr.process(info_message);
|
---|
143 |
|
---|
144 | NodeList service_responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
145 | // check that have same number of responses as collections
|
---|
146 | if (services.getLength() != service_responses.getLength()) {
|
---|
147 | System.err.println("PageAction Error: didn't get a response for each service - somethings gone wrong!");
|
---|
148 | // for now, dont use the metadata
|
---|
149 | } else {
|
---|
150 | for (int i=0; i<services.getLength(); i++) {
|
---|
151 | Element c1 = (Element)services.item(i);
|
---|
152 | Element c2 = (Element)GSXML.getChildByTagName((Element)service_responses.item(i), GSXML.SERVICE_ELEM);
|
---|
153 | if (c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.NAME_ATT))) {
|
---|
154 | //add the service data into the original response
|
---|
155 | GSXML.mergeElements(c1, c2);
|
---|
156 | } else {
|
---|
157 | System.err.println("PageAction Error: response does not correspond to request!");
|
---|
158 | }
|
---|
159 |
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | //now the full response message is in coll_list_response
|
---|
164 | // return the response element
|
---|
165 | Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
|
---|
166 | return response;
|
---|
167 | }
|
---|
168 |
|
---|
169 | // protected Element prefsPage(Element request) {
|
---|
170 |
|
---|
171 | // return null;
|
---|
172 | // }
|
---|
173 |
|
---|
174 | /** if we dont know the page type, use this method */
|
---|
175 | protected Element unknownPage(Element request) {
|
---|
176 |
|
---|
177 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
178 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
179 | String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
180 |
|
---|
181 | // extract the params from the cgi-request,
|
---|
182 | Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
183 | HashMap params = GSXML.extractParams(cgi_paramList, false);
|
---|
184 |
|
---|
185 | String coll_name = (String)params.get(GSParams.COLLECTION);
|
---|
186 | if (coll_name == null || coll_name.equals("")) {
|
---|
187 | // just return an empty response
|
---|
188 | return this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
189 | }
|
---|
190 |
|
---|
191 | // else get the coll description - actually this is the same as for the about page - should we merge these two methods??
|
---|
192 |
|
---|
193 | // if there is a service specified should we get the service description instead??
|
---|
194 | // get the collection or cluster description
|
---|
195 | Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
196 |
|
---|
197 | Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang, uid);
|
---|
198 | coll_about_message.appendChild(coll_about_request);
|
---|
199 |
|
---|
200 | Element coll_about_response = (Element)this.mr.process(coll_about_message);
|
---|
201 |
|
---|
202 | // add the response to the page get teh response element and return it
|
---|
203 | Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
|
---|
204 | return response;
|
---|
205 |
|
---|
206 | }
|
---|
207 | }
|
---|