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.csw.common.util;
025:
026: import java.net.URL;
027: import java.util.ArrayList;
028: import org.fao.geonet.csw.common.Csw;
029: import org.jdom.Element;
030: import org.jdom.Namespace;
031: import java.util.Map;
032: import java.util.HashMap;
033: import java.net.MalformedURLException;
034:
035: //=============================================================================
036:
037: public class CswServer {
038: public static final String GET_RECORDS = "GetRecords";
039: public static final String GET_RECORD_BY_ID = "GetRecordById";
040:
041: //---------------------------------------------------------------------------
042: //---
043: //--- Constructor
044: //---
045: //---------------------------------------------------------------------------
046:
047: public CswServer(Element capab) {
048: parse(capab);
049: }
050:
051: //---------------------------------------------------------------------------
052: //---
053: //--- API methods
054: //---
055: //---------------------------------------------------------------------------
056:
057: public void parse(Element capab) {
058: logs.clear();
059: operations.clear();
060:
061: parseOperations(capab);
062: }
063:
064: //---------------------------------------------------------------------------
065:
066: public Operation getOperation(String name) {
067: return operations.get(name);
068: }
069:
070: //---------------------------------------------------------------------------
071: //---
072: //--- Private methods
073: //---
074: //---------------------------------------------------------------------------
075:
076: private void parseOperations(Element capabil) {
077: Element operMd = capabil.getChild("OperationsMetadata",
078: Csw.NAMESPACE_OWS);
079:
080: if (operMd == null)
081: log("Missing 'ows:OperationsMetadata' element");
082:
083: else
084: for (Object e : operMd.getChildren()) {
085: Element elem = (Element) e;
086:
087: if ("Operation".equals(elem.getName())) {
088: Operation oper = extractOperation(elem);
089:
090: if (oper != null)
091: operations.put(oper.name, oper);
092: }
093: }
094: }
095:
096: //---------------------------------------------------------------------------
097:
098: private Operation extractOperation(Element oper) {
099: String name = oper.getAttributeValue("name");
100:
101: if (name == null) {
102: log("Operation has no 'name' attribute");
103: return null;
104: }
105:
106: Element dcp = oper.getChild("DCP", Csw.NAMESPACE_OWS);
107:
108: if (dcp == null) {
109: log("Missing 'ows:DCP' element in operation");
110: return null;
111: }
112:
113: Element http = dcp.getChild("HTTP", Csw.NAMESPACE_OWS);
114:
115: if (http == null) {
116: log("Missing 'ows:HTTP' element in operation/DCP");
117: return null;
118: }
119:
120: Element get = http.getChild("Get", Csw.NAMESPACE_OWS);
121: Element post = http.getChild("Post", Csw.NAMESPACE_OWS);
122:
123: Operation op = new Operation();
124: op.name = name;
125: op.getUrl = evaluateUrl(get);
126: op.postUrl = evaluateUrl(post);
127:
128: return op;
129: }
130:
131: //---------------------------------------------------------------------------
132:
133: private URL evaluateUrl(Element method) {
134: if (method == null)
135: return null;
136:
137: Namespace ns = Namespace
138: .getNamespace("http://www.w3.org/1999/xlink");
139:
140: String url = method.getAttributeValue("href", ns);
141:
142: if (url == null) {
143: log("Missing 'xlink:href' attribute in operation's http method");
144: return null;
145: }
146:
147: try {
148: return new URL(url);
149: } catch (MalformedURLException e) {
150: log("Malformed 'xlink:href' attribute in operation's http method");
151: return null;
152: }
153: }
154:
155: //---------------------------------------------------------------------------
156:
157: private void log(String message) {
158: logs.add(message);
159: }
160:
161: //---------------------------------------------------------------------------
162: //---
163: //--- Variables
164: //---
165: //---------------------------------------------------------------------------
166:
167: private Map<String, Operation> operations = new HashMap<String, Operation>();
168:
169: private ArrayList<String> logs = new ArrayList<String>();
170:
171: //---------------------------------------------------------------------------
172:
173: public static class Operation {
174: public String name;
175: public URL getUrl;
176: public URL postUrl;
177: }
178: }
179:
180: //=============================================================================
|