001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.geonet.kernel.csw.services;
025:
026: import java.io.File;
027: import java.util.HashMap;
028: import java.util.HashSet;
029: import java.util.Iterator;
030: import java.util.Map;
031: import jeeves.server.context.ServiceContext;
032: import jeeves.utils.Util;
033: import jeeves.utils.Xml;
034: import org.fao.geonet.GeonetContext;
035: import org.fao.geonet.constants.Geonet;
036: import org.fao.geonet.csw.common.Csw;
037: import org.fao.geonet.csw.common.exceptions.CatalogException;
038: import org.fao.geonet.csw.common.exceptions.MissingParameterValueEx;
039: import org.fao.geonet.csw.common.exceptions.NoApplicableCodeEx;
040: import org.fao.geonet.csw.common.exceptions.VersionNegotiationFailedEx;
041: import org.fao.geonet.kernel.csw.CatalogService;
042: import org.fao.geonet.kernel.setting.SettingManager;
043: import org.fao.geonet.lib.Lib;
044: import org.jdom.Element;
045:
046: //=============================================================================
047:
048: public class GetCapabilities extends AbstractOperation implements
049: CatalogService {
050: //---------------------------------------------------------------------------
051: //---
052: //--- Constructor
053: //---
054: //---------------------------------------------------------------------------
055:
056: public GetCapabilities() {
057: }
058:
059: //---------------------------------------------------------------------------
060: //---
061: //--- API methods
062: //---
063: //---------------------------------------------------------------------------
064:
065: public String getName() {
066: return "GetCapabilities";
067: }
068:
069: //---------------------------------------------------------------------------
070:
071: public Element execute(Element request, ServiceContext context)
072: throws CatalogException {
073: if (!checkService(request))
074: throw new MissingParameterValueEx("service");
075:
076: checkService(request);
077: checkAcceptVersions(request);
078:
079: //--- return capabilities
080:
081: String FS = File.separator;
082: String file = context.getAppPath() + "xml" + FS + "csw" + FS
083: + "capabilities.xml";
084:
085: try {
086: Element capabilities = Xml.loadFile(file);
087: substitute(context, capabilities);
088: handleSections(request, capabilities);
089:
090: return capabilities;
091: } catch (Exception e) {
092: context.error("Cannot load/process capabilities");
093: context.error(" (C) StackTrace\n" + Util.getStackTrace(e));
094:
095: throw new NoApplicableCodeEx(
096: "Cannot load/process capabilities");
097: }
098: }
099:
100: //---------------------------------------------------------------------------
101:
102: public Element adaptGetRequest(Map<String, String> params) {
103: String service = params.get("service");
104: String sections = params.get("sections");
105: String sequence = params.get("updatesequence");
106: String acceptVers = params.get("acceptversions");
107: String acceptForm = params.get("acceptformats");
108:
109: Element request = new Element(getName(), Csw.NAMESPACE_CSW);
110:
111: setAttrib(request, "service", service);
112: setAttrib(request, "updateSequence", sequence);
113:
114: fill(request, "AcceptVersions", "Version", acceptVers,
115: Csw.NAMESPACE_OWS);
116: fill(request, "Sections", "Section", sections,
117: Csw.NAMESPACE_OWS);
118: fill(request, "AcceptFormats", "OutputFormat", acceptForm,
119: Csw.NAMESPACE_OWS);
120:
121: return request;
122: }
123:
124: //---------------------------------------------------------------------------
125: //---
126: //--- Private methods
127: //---
128: //---------------------------------------------------------------------------
129:
130: private void checkAcceptVersions(Element request)
131: throws CatalogException {
132: Element versions = request.getChild("AcceptVersions",
133: Csw.NAMESPACE_OWS);
134:
135: if (versions == null)
136: return;
137:
138: Iterator i = versions.getChildren().iterator();
139:
140: StringBuffer sb = new StringBuffer();
141:
142: while (i.hasNext()) {
143: Element version = (Element) i.next();
144:
145: if (version.getText().equals(Csw.CSW_VERSION))
146: return;
147:
148: sb.append(version.getText());
149:
150: if (i.hasNext())
151: sb.append(",");
152: }
153:
154: throw new VersionNegotiationFailedEx(sb.toString());
155: }
156:
157: //---------------------------------------------------------------------------
158:
159: private void handleSections(Element request, Element capabilities) {
160: Element sections = request.getChild("Sections",
161: Csw.NAMESPACE_OWS);
162:
163: if (sections == null)
164: return;
165:
166: //--- handle 'section' parameters
167:
168: HashSet<String> hsSections = new HashSet<String>();
169:
170: Iterator i = sections.getChildren().iterator();
171:
172: while (i.hasNext()) {
173: Element section = (Element) i.next();
174: hsSections.add(section.getText());
175: }
176:
177: if (hsSections.size() == 0) {
178: capabilities.getChild("ServiceIdentification",
179: Csw.NAMESPACE_OWS).detach();
180: capabilities.getChild("ServiceProvider", Csw.NAMESPACE_OWS)
181: .detach();
182: capabilities.getChild("OperationsMetadata",
183: Csw.NAMESPACE_OWS).detach();
184:
185: return;
186: }
187:
188: //--- remove not requested sections
189:
190: if (!hsSections.contains("ServiceIdentification"))
191: capabilities.getChild("ServiceIdentification",
192: Csw.NAMESPACE_OWS).detach();
193:
194: if (!hsSections.contains("ServiceProvider"))
195: capabilities.getChild("ServiceProvider", Csw.NAMESPACE_OWS)
196: .detach();
197:
198: if (!hsSections.contains("OperationsMetadata"))
199: capabilities.getChild("OperationsMetadata",
200: Csw.NAMESPACE_OWS).detach();
201:
202: //--- the filter section is mandatory
203: // if (!hsSections.contains("Filter_Capabilities"))
204: // capabilities.getChild("Filter_Capabilities", Csw.NAMESPACE_OGC).detach();
205: }
206:
207: //---------------------------------------------------------------------------
208:
209: private void substitute(ServiceContext context, Element capab)
210: throws Exception {
211: GeonetContext gc = (GeonetContext) context
212: .getHandlerContext(Geonet.CONTEXT_NAME);
213: SettingManager sm = gc.getSettingManager();
214:
215: HashMap<String, String> vars = new HashMap<String, String>();
216:
217: vars.put("$HOST", sm.getValue("system/server/host"));
218: vars.put("$PORT", sm.getValue("system/server/port"));
219: vars.put("$SERVLET", context.getBaseUrl());
220:
221: Lib.element.substitute(capab, vars);
222: }
223: }
224:
225: //=============================================================================
|