001    /*
002     * 
003     * $Revision: 14016 $ $Date: 2008-09-18 12:07:06 +0200 (Do, 18 Sep 2008) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.services.fieldquery;
025    
026    import java.net.URL;
027    
028    import javax.xml.namespace.QName;
029    
030    import org.apache.axis.client.Call;
031    import org.apache.axis.client.Service;
032    import org.apache.axis.constants.Style;
033    import org.apache.axis.constants.Use;
034    import org.apache.axis.description.OperationDesc;
035    import org.apache.axis.description.ParameterDesc;
036    
037    import org.jdom.Document;
038    import org.jdom.input.DOMBuilder;
039    
040    /*
041     * This class is the implementation for remote access via Webservices
042     * 
043     * @author Jens Kupferschmidt
044     * @author Frank L\u00fctzenkirchen
045     */
046    
047    public class MCRQueryClientWebService extends MCRQueryClientBase {
048    
049        /** The AXIS service object */
050        protected static Service service = new Service();
051    
052        /** The description of the doQuery service operation */
053        protected static OperationDesc operation;
054    
055        /** The complete WebService URL * */
056        protected String endpoint = "";
057    
058        static {
059            String xmlsoap = "http://xml.apache.org/xml-soap";
060    
061            // Build doQuery operation description
062            operation = new OperationDesc();
063            operation.setName("MCRDoQuery");
064            operation.addParameter(new ParameterDesc(new QName("", "in0"), ParameterDesc.IN, new QName("http://xml.apache.org/xml-soap", "Document"), org.w3c.dom.Document.class, false, false));
065            operation.setReturnType(new QName(xmlsoap, "Document"));
066            operation.setReturnClass(org.w3c.dom.Document.class);
067            operation.setReturnQName(new QName("", "MCRDoQueryReturn"));
068            operation.setStyle(Style.RPC);
069            operation.setUse(Use.ENCODED);
070        }
071    
072        /*
073         * The constructor.
074         */
075        public MCRQueryClientWebService() {
076            super();
077        }
078    
079        /*
080         * The initialization.
081         * 
082         * @param xmlhost an entry of a remote host from hosts.xml
083         */
084        public void init(org.jdom.Element xmlhost) {
085            alias = xmlhost.getAttributeValue("alias");
086            if ((alias == null) || ((alias = alias.trim()).length() == 0)) {
087                alias = "remote";
088                LOGGER.warn("The alias attribute for the host is null or empty, remote was set.");
089            }
090            url = xmlhost.getAttributeValue("url");
091            if ((url == null) || ((url = url.trim()).length() == 0)) {
092                url = "http://localhost:8291/";
093                LOGGER.warn("The url attribute for the host is null or empty, http://localhost:8291/ was set.");
094            }
095            access = xmlhost.getAttributeValue("access");
096            if ((access == null) || ((access = access.trim()).length() == 0)) {
097                alias = "webservice";
098                LOGGER.warn("The access attribute for the host is null or empty, webservice was set.");
099            }
100            servicepath = xmlhost.getAttributeValue("servicepath");
101            if ((servicepath == null) || ((servicepath = servicepath.trim()).length() == 0)) {
102                alias = "services/MCRWebService";
103                LOGGER.warn("The servicepath attribute for the host is null or empty, services/MCRWebService was set.");
104            }
105            StringBuffer sb = new StringBuffer(256);
106            sb.append("Host ").append(alias).append(" with access mode ").append(access).append(" uses host url ").append(url).append(servicepath);
107            LOGGER.debug(sb.toString());
108            if (!url.endsWith("/"))
109                url = url + "/";
110            endpoint = url + servicepath;
111        }
112    
113        /**
114         * Executes a query on a single remote host using the defined service.
115         * 
116         * @param inDoc
117         *            the query as W3C DOM document
118         * @param results
119         *            the result list to add the hits to
120         */
121        public void search(org.w3c.dom.Document inDoc, MCRResults results) {
122            try {
123                // Build webservice call
124                Call call = (Call) (service.createCall());
125                call.setTargetEndpointAddress(new URL(endpoint));
126                call.setOperation(operation);
127                call.setOperationName("MCRDoQuery");
128    
129                // Call webservice
130                org.w3c.dom.Document outDoc = (org.w3c.dom.Document) (call.invoke(new Object[] { inDoc }));
131                LOGGER.info("Received remote query results, processing XML now");
132    
133                // Process xml response
134                Document response = new DOMBuilder().build(outDoc);
135                int numHits = results.merge(response, alias);
136                results.setHostConnection(alias, "");
137                LOGGER.debug("Received " + numHits + " hits from host " + alias);
138            } catch (Exception ex) {
139                String msg = "Exception while querying remote host " + alias;
140                LOGGER.error(msg, ex);
141                results.setHostConnection(alias, msg+ " : "+ex.getLocalizedMessage());
142            }
143        }
144    
145        /**
146         * Retrieves an Object from remote host using the defined service.
147         * 
148         * @param hostAlias
149         *            the alias of the remote host as defined in hosts.xml
150         * @param ID
151         *            the ID of the object to retrieve
152         * @return the object document
153         */
154        public org.w3c.dom.Document doRetrieveObject(String ID) {
155            try {
156                // Build webservice call
157                Call call = (Call) (service.createCall());
158                call.setTargetEndpointAddress(new URL(endpoint));
159                call.setOperation(operation);
160                call.setOperationName("MCRDoRetrieveObject");
161                // Call webservice
162                org.w3c.dom.Document outDoc = (org.w3c.dom.Document) (call.invoke(new Object[] { ID }));
163                LOGGER.info("Received remote Object: " + ID);
164    
165                return outDoc;
166            } catch (Exception ex) {
167                String msg = "Exception while retrieving Object '" + ID + "' from remote host " + alias;
168                LOGGER.error(msg, ex);
169            }
170            return null;
171        }
172    
173        /**
174         * Retrieves an classification part from remote host using the WebService.
175         * 
176         * @param level
177         *            the level of the classification to retrieve
178         * @param type
179         *            the type of the classification to retrieve
180         * @param classID
181         *            the class ID of the classification to retrieve
182         * @param categID
183         *            the category ID of the classification to retrieve
184         * @param format
185         *            of retrieved classification, valid values are:
186         *            editor['['formatAlias']']|metadata
187         * @return the classification document
188         */
189        public org.w3c.dom.Document doRetrieveClassification(String level, String type, String classID, String categID, String format) {
190            StringBuffer ID = new StringBuffer(256);
191            ID.append("level=").append(level).append(":type=").append(type).append(":classId=").append(classID).append(":categId=").append(categID);
192            try {
193                // Build webservice call
194                Call call = (Call) (service.createCall());
195                call.setTargetEndpointAddress(new URL(endpoint));
196                call.setOperation(operation);
197                call.setOperationName("MCRDoRetrieveClassification");
198                call.removeAllParameters();
199                call.addParameter("level", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
200                call.addParameter("type", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
201                call.addParameter("classID", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
202                call.addParameter("categID", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
203                call.addParameter("format", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
204                call.setReturnType(new QName("http://xml.apache.org/xml-soap", "Document"));
205                // Call webservice
206                org.w3c.dom.Document outDoc = (org.w3c.dom.Document) (call.invoke(new Object[] { level, type, classID, categID, format }));
207                LOGGER.info("Received remote Object: " + ID.toString());
208    
209                return outDoc;
210            } catch (Exception ex) {
211                String msg = "Exception while retrieving Object '" + ID.toString() + "' from remote host " + alias;
212                LOGGER.error(msg, ex);
213            }
214            return null;
215        }
216    
217        /**
218         * Retrieves an link from remote host using the WebService.
219         * 
220         * @param hostAlias
221         *            the alias of the remote host as defined in hosts.xml
222         * @param from
223         *            the source of the link
224         * @param to
225         *            the target of the link
226         * @param type
227         *            the type of the link
228         * @return the mcr:result document
229         */
230        public org.w3c.dom.Document doRetrieveLinks(String from, String to, String type) {
231            StringBuffer ID = new StringBuffer(256);
232            ID.append("from=").append(from).append(":to=").append(to).append(":type=").append(type);
233            try {
234                // Build webservice call
235                Call call = (Call) (service.createCall());
236                call.setTargetEndpointAddress(new URL(endpoint));
237                call.setOperation(operation);
238                call.setOperationName("MCRDoRetrieveLinks");
239                call.removeAllParameters();
240                call.addParameter("from", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
241                call.addParameter("to", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
242                call.addParameter("type", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
243                call.setReturnType(new QName("http://xml.apache.org/xml-soap", "Document"));
244                // Call webservice
245                org.w3c.dom.Document outDoc = (org.w3c.dom.Document) (call.invoke(new Object[] { from, to, type }));
246                LOGGER.info("Received remote Object: " + ID.toString());
247    
248                return outDoc;
249            } catch (Exception ex) {
250                String msg = "Exception while retrieving Object '" + ID.toString() + "' from remote host " + alias;
251                LOGGER.error(msg, ex);
252            }
253            return null;
254        }
255    }