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.map.layers;
025:
026: import java.net.HttpURLConnection;
027: import java.net.URL;
028: import java.util.ArrayList;
029: import java.util.List;
030: import jeeves.interfaces.Service;
031: import jeeves.server.ServiceConfig;
032: import jeeves.server.context.ServiceContext;
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.MapService;
037: import org.wfp.vam.intermap.kernel.map.mapServices.wms.WmsService;
038: import org.wfp.vam.intermap.kernel.map.mapServices.wms.schema.type.WMSFormat;
039: import org.wfp.vam.intermap.kernel.map.mapServices.wms.schema.type.WMSLayer;
040: import org.wfp.vam.intermap.kernel.map.mapServices.wms.schema.type.WMSMetadataURL;
041: import org.wfp.vam.intermap.services.map.MapUtil;
042:
043: public class GetInfo implements Service {
044: public void init(String appPath, ServiceConfig config)
045: throws Exception {
046: }
047:
048: static class PreferredInfo {
049: WMSMetadataURL.MDTYPE _type;
050: WMSFormat _format;
051:
052: public PreferredInfo(WMSMetadataURL.MDTYPE type,
053: WMSFormat format) {
054: _type = type;
055: _format = format;
056: }
057:
058: public boolean match(WMSMetadataURL mdurl) {
059: return _type == mdurl.getType()
060: && _format == mdurl.getFormat();
061: }
062: }
063:
064: private static List<PreferredInfo> _preferred = new ArrayList<PreferredInfo>();
065:
066: static {
067: _preferred.add(new PreferredInfo(
068: WMSMetadataURL.MDTYPE.ISO19115, WMSFormat.TEXT_HTML));
069: _preferred.add(new PreferredInfo(
070: WMSMetadataURL.MDTYPE.ISO19115, WMSFormat.TEXT_XHTML));
071: _preferred.add(new PreferredInfo(
072: WMSMetadataURL.MDTYPE.ISO19115, WMSFormat.TEXT_PLAIN));
073: _preferred.add(new PreferredInfo(WMSMetadataURL.MDTYPE.FGDC,
074: WMSFormat.TEXT_HTML));
075: _preferred.add(new PreferredInfo(WMSMetadataURL.MDTYPE.FGDC,
076: WMSFormat.TEXT_XHTML));
077: _preferred.add(new PreferredInfo(WMSMetadataURL.MDTYPE.FGDC,
078: WMSFormat.TEXT_PLAIN));
079: }
080:
081: //--------------------------------------------------------------------------
082: //---
083: //--- Service
084: //---
085: //--------------------------------------------------------------------------
086:
087: public Element exec(Element params, ServiceContext context)
088: throws Exception {
089: int id = Integer.parseInt(params
090: .getChildText(Constants.MAP_SERVICE_ID));
091: MapMerger mm = MapUtil.getMapMerger(context);
092:
093: Element ret = new Element("response");
094:
095: MapService ms = mm.getService(id);
096:
097: if (ms instanceof WmsService) {
098: WmsService ws = (WmsService) ms;
099: WMSLayer wlayer = ws.getWmsLayer();
100:
101: ret.addContent(new Element("type").setText("WMS"));
102: ret.addContent(new Element("title").setText(wlayer
103: .getTitle()));
104: ret.addContent(new Element("abstract").setText(wlayer
105: .getAbstract()));
106:
107: String legendURL = ws.getLegendUrl();
108: if (legendURL != null)
109: ret.addContent(new Element("legendURL")
110: .setText(legendURL));
111:
112: WMSMetadataURL md = null;
113: for (PreferredInfo pref : _preferred) {
114: md = getMetadata(wlayer, pref);
115: if (md != null)
116: break;
117: }
118:
119: if (md != null) {
120: String href = md.getOnlineResource().getHref();
121: URL url = new URL(href);
122: HttpURLConnection conn = (HttpURLConnection) url
123: .openConnection();
124: String info = (String) conn.getContent(); // preferred info are all text
125: ret.addContent(new Element("info").setText(info)
126: .setAttribute("format",
127: md.getFormat().toString())
128: .setAttribute("type", md.getType().toString()));
129: }
130: } else {
131: ret.addContent(new Element("type").setText("ARCIMS"));
132: ret.addContent(new Element("title").setText(ms.getTitle()));
133: }
134:
135: return ret;
136: }
137:
138: private WMSMetadataURL getMetadata(WMSLayer wlayer,
139: PreferredInfo pref) {
140: for (WMSMetadataURL wmdurl : wlayer.getMetadataURLIterator()) {
141: if (pref.match(wmdurl))
142: return wmdurl;
143: }
144: return null;
145: }
146:
147: }
|