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 |
|
---|
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 = 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 { // unknown page
|
---|
34 |
|
---|
35 | System.err.println("PageAction Error: unknown page specified!");
|
---|
36 | response = unknownPage(request);
|
---|
37 | }
|
---|
38 |
|
---|
39 | result.appendChild(doc_.importNode(response, true));
|
---|
40 | return result;
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | protected Element homePage(Element request) {
|
---|
45 |
|
---|
46 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
47 |
|
---|
48 | // first, get the message router info
|
---|
49 | Element coll_list_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
50 | Element coll_list_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, "", lang);
|
---|
51 | coll_list_message.appendChild(coll_list_request);
|
---|
52 | Element coll_list_response = (Element)mr_.process(coll_list_message);
|
---|
53 | if (coll_list_response==null) {
|
---|
54 | System.err.println("PageAction Error: couldn't query the message router!");
|
---|
55 | return null;
|
---|
56 | }
|
---|
57 |
|
---|
58 | // second, get the metadata for each collection - we only want specific
|
---|
59 | // elements but for now, we'll just get it all
|
---|
60 | NodeList colls = coll_list_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
61 | // we will send all the requests in a single message
|
---|
62 | Element metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
63 | for (int i=0; i<colls.getLength(); i++) {
|
---|
64 | Element c = (Element)colls.item(i);
|
---|
65 | String name = c.getAttribute(GSXML.NAME_ATT);
|
---|
66 |
|
---|
67 | Element metadata_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, name, lang);
|
---|
68 | metadata_message.appendChild(metadata_request);
|
---|
69 | }
|
---|
70 |
|
---|
71 | Element metadata_response = (Element)mr_.process(metadata_message);
|
---|
72 |
|
---|
73 | NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
74 | // check that have same number of responses as collections
|
---|
75 | if (colls.getLength() != coll_responses.getLength()) {
|
---|
76 | System.err.println("PageAction Error: didn't get a response for each collection - somethings gone wrong!");
|
---|
77 | // for now, dont use the metadata
|
---|
78 | } else {
|
---|
79 | for (int i=0; i<colls.getLength(); i++) {
|
---|
80 | Element c1 = (Element)colls.item(i);
|
---|
81 | Element c2 = (Element)coll_responses.item(i);
|
---|
82 | if (c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.FROM_ATT))) {
|
---|
83 | // add the metadata to the original response
|
---|
84 | GSXML.mergeMetadataLists(c1, GSXML.getChildByTagName(c2, GSXML.COLLECTION_ELEM));
|
---|
85 | } else {
|
---|
86 | System.err.println("PageAction Error: response does not correspond to request!");
|
---|
87 | }
|
---|
88 |
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | //now the full response message is in coll_list_response
|
---|
93 | // return the response element
|
---|
94 | Element response = (Element) GSXML.getChildByTagName(coll_list_response, GSXML.RESPONSE_ELEM);
|
---|
95 | return response;
|
---|
96 |
|
---|
97 | } // homePage
|
---|
98 |
|
---|
99 | protected Element aboutPage(Element request) {
|
---|
100 |
|
---|
101 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
102 | // extract the params from the cgi-request,
|
---|
103 | Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
104 | HashMap params = GSXML.extractParams(cgi_paramList, false);
|
---|
105 |
|
---|
106 | String coll_name = (String)params.get(GSCGI.COLLECTION_ARG);
|
---|
107 | if (coll_name == null || coll_name.equals("")) {
|
---|
108 | System.err.println("PageAction Error: about page requested with no collection or cluster specified!");
|
---|
109 | // return an empty response
|
---|
110 | return doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
111 | }
|
---|
112 |
|
---|
113 | // get the collection or cluster description
|
---|
114 | Element coll_about_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
115 |
|
---|
116 | Element coll_about_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang);
|
---|
117 | coll_about_message.appendChild(coll_about_request);
|
---|
118 |
|
---|
119 | Element coll_about_response = (Element)mr_.process(coll_about_message);
|
---|
120 |
|
---|
121 | // get the response element and return it
|
---|
122 | Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
|
---|
123 | return response;
|
---|
124 |
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** if we dont know the page type, use this method */
|
---|
128 | protected Element unknownPage(Element request) {
|
---|
129 |
|
---|
130 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
131 | String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
132 |
|
---|
133 | // extract the params from the cgi-request,
|
---|
134 | Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
135 | HashMap params = GSXML.extractParams(cgi_paramList, false);
|
---|
136 |
|
---|
137 | String coll_name = (String)params.get(GSCGI.COLLECTION_ARG);
|
---|
138 | if (coll_name == null || coll_name.equals("")) {
|
---|
139 | // just return an empty response
|
---|
140 | return doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
141 | }
|
---|
142 |
|
---|
143 | // else get the coll description - actually this is the same as for the about page - should we merge these two methods??
|
---|
144 |
|
---|
145 | // if there is a service specified should we get the service description instead??
|
---|
146 | // get the collection or cluster description
|
---|
147 | Element coll_about_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
148 |
|
---|
149 | Element coll_about_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang);
|
---|
150 | coll_about_message.appendChild(coll_about_request);
|
---|
151 |
|
---|
152 | Element coll_about_response = (Element)mr_.process(coll_about_message);
|
---|
153 |
|
---|
154 | // add the response to the page get teh response element and return it
|
---|
155 | Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
|
---|
156 | return response;
|
---|
157 |
|
---|
158 | }
|
---|
159 | }
|
---|