source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/PageAction.java@ 4980

Last change on this file since 4980 was 4950, checked in by kjdon, 21 years ago

now about action gets the service descriptions for each service, so it can display the service name rather than internal id

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