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.services.main;
025:
026: import java.net.URL;
027: import java.util.List;
028: import jeeves.exceptions.MissingParameterEx;
029: import jeeves.exceptions.UserNotFoundEx;
030: import jeeves.interfaces.Service;
031: import jeeves.server.ServiceConfig;
032: import jeeves.server.context.ServiceContext;
033: import jeeves.utils.Util;
034: import jeeves.utils.XmlRequest;
035: import org.fao.geonet.constants.Geonet;
036: import org.fao.geonet.lib.Lib;
037: import org.jdom.Element;
038:
039: //=============================================================================
040:
041: public class Forward implements Service {
042: //--------------------------------------------------------------------------
043: //---
044: //--- Init
045: //---
046: //--------------------------------------------------------------------------
047:
048: public void init(String appPath, ServiceConfig config)
049: throws Exception {
050: }
051:
052: //--------------------------------------------------------------------------
053: //---
054: //--- Service
055: //---
056: //--------------------------------------------------------------------------
057:
058: public Element exec(Element params, ServiceContext context)
059: throws Exception {
060: Element site = Util.getChild(params, "site");
061: Element par = Util.getChild(params, "params");
062: Element acc = site.getChild("account");
063:
064: String url = Util.getParam(site, "url");
065: String type = Util.getParam(site, "type", "generic");
066:
067: String username = (acc == null) ? null : Util.getParam(acc,
068: "username");
069: String password = (acc == null) ? null : Util.getParam(acc,
070: "password");
071:
072: List list = par.getChildren();
073:
074: if (list.size() == 0)
075: throw new MissingParameterEx("<request>", par);
076:
077: params = (Element) list.get(0);
078:
079: XmlRequest req = new XmlRequest(new URL(url));
080:
081: //--- does we need to authenticate?
082:
083: if (username != null)
084: authenticate(req, username, password, type);
085:
086: Lib.net.setupProxy(context, req);
087: req.setRequest(params);
088:
089: return req.execute();
090: }
091:
092: //--------------------------------------------------------------------------
093:
094: private void authenticate(XmlRequest req, String username,
095: String password, String type) throws Exception {
096: if (!type.equals("geonetwork"))
097: //--- set basic/digest credentials for non geonetwork sites
098: req.setCredentials(username, password);
099:
100: else {
101: String addr = req.getAddress();
102: int pos = addr.lastIndexOf("/");
103:
104: req.setAddress(addr.substring(0, pos + 1)
105: + Geonet.Service.XML_LOGIN);
106: req.addParam("username", username);
107: req.addParam("password", password);
108:
109: Element response = req.execute();
110:
111: if (!response.getName().equals("ok"))
112: throw new UserNotFoundEx(username);
113:
114: req.setAddress(addr);
115: }
116: }
117: }
118:
119: //=============================================================================
|