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.wfp.vam.intermap.services.wmc;
025:
026: import java.io.BufferedInputStream;
027: import java.net.HttpURLConnection;
028: import java.net.URL;
029: import jeeves.interfaces.Service;
030: import jeeves.server.ServiceConfig;
031: import jeeves.server.context.ServiceContext;
032: import jeeves.utils.Xml;
033: import org.jdom.Element;
034: import org.wfp.vam.intermap.Constants;
035: import org.wfp.vam.intermap.kernel.map.MapMerger;
036: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.GeoRSSCodec;
037: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.schema.impl.WMCFactory;
038: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.schema.type.WMCExtension;
039: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.schema.type.WMCViewContext;
040: import org.wfp.vam.intermap.kernel.map.mapServices.wmc.schema.type.WMCWindow;
041: import org.wfp.vam.intermap.kernel.map.mapServices.wms.schema.impl.Utils;
042: import org.wfp.vam.intermap.kernel.marker.MarkerSet;
043: import org.wfp.vam.intermap.services.map.MapUtil;
044:
045: /**
046: * Set the WMC from an URL-retrieved document
047: *
048: * @author ETj
049: */
050: public class SetWmcFromURL implements Service {
051: public void init(String appPath, ServiceConfig config)
052: throws Exception {
053: }
054:
055: //--------------------------------------------------------------------------
056: //---
057: //--- Service
058: //---
059: //--------------------------------------------------------------------------
060:
061: public Element exec(Element params, ServiceContext context)
062: throws Exception {
063: String url = params.getChildText("url");
064:
065: URL u = new URL(url);
066: HttpURLConnection conn = (HttpURLConnection) u.openConnection();
067: BufferedInputStream is = new BufferedInputStream(conn
068: .getInputStream());
069: Element mapContext = Xml.loadStream(is);
070: conn.disconnect();
071:
072: // XMLOutputter xo = new XMLOutputter(Format.getPrettyFormat());
073: // System.out.println(" ============= wmc is:\n\n" +xo.outputString(mapContext));
074:
075: // Create a new MapMerger object
076: String sreplace = params.getChildText("clear");
077: boolean breplace = Utils.getBooleanAttrib(sreplace, true);
078:
079: MapMerger mm = breplace ? new MapMerger() : MapUtil
080: .getMapMerger(context);
081:
082: WMCViewContext vc = WMCFactory.parseViewContext(mapContext);
083: WMCWindow win = vc.getGeneral().getWindow();
084:
085: String imgurl = MapUtil.setContext(mm, vc);
086:
087: // Update the user session
088: context.getUserSession().setProperty(Constants.SESSION_MAP, mm);
089:
090: // load markerset, if any, from context
091: WMCExtension ext = vc.getGeneral().getExtension();
092: if (ext != null) {
093: Element georss = ext.getChild("georss");
094: if (georss != null) {
095: Element feed = (Element) georss.getChildren().get(0);
096: MarkerSet ms = GeoRSSCodec.parseGeoRSS(feed);
097: context.getUserSession().setProperty(
098: Constants.SESSION_MARKERSET, ms);
099: }
100: }
101:
102: // Prepare response
103: Element response = new Element("response").addContent(
104: new Element("imgUrl").setText(url)).addContent(
105: new Element("scale").setText(mm.getDistScale()))
106: .addContent(mm.getBoundingBox().toElement())
107: .addContent(
108: new Element("width").setText(""
109: + win.getWidth())).addContent(
110: new Element("height").setText(""
111: + win.getHeight()));
112:
113: MarkerSet ms = (MarkerSet) context.getUserSession()
114: .getProperty(Constants.SESSION_MARKERSET);
115: if (ms != null)
116: response.addContent(ms.select(mm.getBoundingBox())
117: .toElement());
118:
119: return response;
120: }
121: }
122:
123: //
124: // 2007 11 22 ETj : following there is the old version of this service.
125: // It handles some non-standard elements, and is deprecated.
126: // I leave it here in case we need to restore it in a hurry, but it has to be removed entirely.
127: //
128:
129: //=============================================================================
130:
131: /** main.result service. shows search results
132: */
133:
134: //public class SetWmcFromURL implements Service
135: //{
136: // public void init(String appPath, ServiceConfig config) throws Exception {}
137: //
138: // //--------------------------------------------------------------------------
139: // //---
140: // //--- Service
141: // //---
142: // //--------------------------------------------------------------------------
143: //
144: // /**
145: // * Method exec
146: // *
147: // * @param params an Element
148: // * @param context a ServiceContext
149: // *
150: // * @return an Element
151: // *
152: // * @exception Exception
153: // *
154: // */
155: // public Element exec(Element params, ServiceContext context) throws Exception
156: // {
157: // String url = params.getChildText("url");
158: //
159: // URL u = new URL(url);
160: // HttpURLConnection conn = (HttpURLConnection)u.openConnection();
161: // BufferedInputStream is = new BufferedInputStream(conn.getInputStream());
162: // Element mapContext = Xml.loadStream(is);
163: // conn.disconnect();
164: //
165: // String stContext = mapContext.getText();
166: // String decoded = URLDecoder.decode(stContext);
167: //
168: // mapContext = Xml.loadString(decoded, false);
169: //
170: // // Create a new MapMerger object
171: // MapMerger mm = addContextLayers(mapContext); // DEBUG
172: //
173: // // Set image size if not set
174: // String size = (String)context.getUserSession().getProperty(Constants.SESSION_SIZE);
175: // if (size == null)
176: // context.getUserSession().setProperty(Constants.SESSION_SIZE, "small");
177: //
178: // // Update the user session
179: // context.getUserSession().setProperty(Constants.SESSION_MAP, mm);
180: //
181: // return new Element("response");
182: // }
183: //
184: // public static MapMerger addContextLayers(Element context) throws Exception
185: // {
186: // MapMerger mm = new MapMerger();
187: //
188: // // Add each layer in the context to the map
189: // Namespace ns = org.jdom.Namespace.getNamespace("http://www.opengeospatial.net/context");
190: // List layers = context.getChild("LayerList", ns).getChildren("Layer", ns);
191: //
192: // for (Iterator i = layers.iterator(); i.hasNext(); )
193: // {
194: // Element layer = (Element)i.next();
195: //
196: // int serverType = 2;
197: // Element olr = layer.getChild("Server", ns).getChild("OnlineResource", ns);
198: //
199: // Namespace linkNs = Namespace.getNamespace("xlink", "http://www.w3.org/1999/xlink");
200: // String serverUrl = olr.getAttributeValue("href", linkNs);
201: // String serviceName = layer.getChildText("Name", ns);
202: // String vsp = layer.getChildText("vendor_spec_par"); // vendor specific parameters
203: //
204: // try {
205: // MapUtil.addService(serverType, serverUrl, serviceName, vsp, mm);
206: // } catch (Exception e) { e.printStackTrace(); } // DEBUG
207: // }
208: //
209: // mm.setBoundingBox(new BoundingBox());
210: // return mm;
211: // }
212: //
213: //}
214: //=============================================================================
|