Mercurial > hg > mpdl-group
comparison software/eXist/mpdl-modules/src/org/exist/xquery/modules/mpdldoc/CheckUri.java @ 0:408254cf2f1d
Erstellung
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 24 Nov 2010 17:24:23 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:408254cf2f1d |
---|---|
1 /* | |
2 * eXist Open Source Native XML Database: Extension module | |
3 * Copyright (C) 2008 Josef Willenborg | |
4 * jwillenborg@mpiwg-berlin.mpg.de | |
5 * http://www.mpiwg-berlin.mpg.de | |
6 * | |
7 * This program is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public License | |
9 * as published by the Free Software Foundation; either version 2 | |
10 * of the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 * | |
21 * $Id: $ | |
22 */ | |
23 package org.exist.xquery.modules.mpdldoc; | |
24 | |
25 import java.io.IOException; | |
26 import org.apache.commons.httpclient.HttpClient; | |
27 import org.apache.commons.httpclient.methods.GetMethod; | |
28 import org.apache.commons.httpclient.params.HttpClientParams; | |
29 import org.apache.commons.httpclient.params.HttpMethodParams; | |
30 import org.exist.dom.QName; | |
31 import org.exist.xquery.BasicFunction; | |
32 import org.exist.xquery.Cardinality; | |
33 import org.exist.xquery.FunctionSignature; | |
34 import org.exist.xquery.XPathException; | |
35 import org.exist.xquery.XQueryContext; | |
36 import org.exist.xquery.value.BooleanValue; | |
37 import org.exist.xquery.value.NumericValue; | |
38 import org.exist.xquery.value.Sequence; | |
39 import org.exist.xquery.value.SequenceType; | |
40 import org.exist.xquery.value.Type; | |
41 | |
42 /** | |
43 * @author Josef Willenborg (jwillenborg@mpiwg-berlin.mpg.de) | |
44 */ | |
45 public class CheckUri extends BasicFunction { | |
46 | |
47 public final static FunctionSignature signature = | |
48 new FunctionSignature( | |
49 new QName("check-uri", MPDLDocModule.NAMESPACE_URI, MPDLDocModule.PREFIX), | |
50 "A function which checks the uri-string if it is available within a timeout value (in ms).", | |
51 new SequenceType[] { | |
52 new SequenceType(Type.STRING, Cardinality.ZERO_OR_ONE), | |
53 new SequenceType(Type.INTEGER, Cardinality.ZERO_OR_ONE) | |
54 }, | |
55 new SequenceType(Type.BOOLEAN, Cardinality.EXACTLY_ONE)); | |
56 | |
57 public CheckUri(XQueryContext context) { | |
58 super(context, signature); | |
59 } | |
60 | |
61 public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { | |
62 Sequence isOk = BooleanValue.TRUE; | |
63 HttpClient httpClient = new HttpClient(); | |
64 GetMethod method = null; | |
65 try { | |
66 Sequence firstSeq = args[0]; | |
67 Sequence secondSeq = args[1]; | |
68 if (firstSeq.isEmpty()) | |
69 return isOk; | |
70 String uriStr = firstSeq.getStringValue(); | |
71 int milliseconds = 2000; // default value | |
72 if (! secondSeq.isEmpty()) { | |
73 NumericValue value = (NumericValue) secondSeq.convertTo(Type.NUMBER); | |
74 milliseconds = value.getInt(); | |
75 } | |
76 httpClient.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(milliseconds)); | |
77 httpClient.getParams().setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, new Long(milliseconds)); | |
78 method = new GetMethod(uriStr); | |
79 method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(milliseconds)); | |
80 method.setFollowRedirects(true); | |
81 httpClient.executeMethod(method); | |
82 } catch (IOException e) { | |
83 isOk = BooleanValue.FALSE; // if timeout exception is thrown | |
84 } finally { | |
85 if (method != null) { | |
86 method.releaseConnection(); | |
87 } | |
88 } | |
89 return isOk; | |
90 } | |
91 } |