KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > Http


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.condition;
20
21 import java.net.HttpURLConnection JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.ProjectComponent;
28
29 /**
30  * Condition to wait for a HTTP request to succeed. Its attribute(s) are:
31  * url - the URL of the request.
32  * errorsBeginAt - number at which errors begin at; default=400.
33  * @since Ant 1.5
34  */

35 public class Http extends ProjectComponent implements Condition {
36     private static final int ERROR_BEGINS = 400;
37     private String JavaDoc spec = null;
38
39     /**
40      * Set the url attribute
41      * @param url the url of the request
42      */

43     public void setUrl(String JavaDoc url) {
44         spec = url;
45     }
46
47     private int errorsBeginAt = ERROR_BEGINS;
48
49     /**
50      * Set the errorsBeginAt attribute
51      * @param errorsBeginAt number at which errors begin at, default is
52      * 400
53      */

54     public void setErrorsBeginAt(int errorsBeginAt) {
55         this.errorsBeginAt = errorsBeginAt;
56     }
57
58     /**
59      * @return true if the HTTP request succeeds
60      * @exception BuildException if an error occurs
61      */

62     public boolean eval() throws BuildException {
63         if (spec == null) {
64             throw new BuildException("No url specified in http condition");
65         }
66         log("Checking for " + spec, Project.MSG_VERBOSE);
67         try {
68             URL JavaDoc url = new URL JavaDoc(spec);
69             try {
70                 URLConnection JavaDoc conn = url.openConnection();
71                 if (conn instanceof HttpURLConnection JavaDoc) {
72                     HttpURLConnection JavaDoc http = (HttpURLConnection JavaDoc) conn;
73                     int code = http.getResponseCode();
74                     log("Result code for " + spec + " was " + code,
75                         Project.MSG_VERBOSE);
76                     if (code > 0 && code < errorsBeginAt) {
77                         return true;
78                     }
79                     return false;
80                 }
81             } catch (java.io.IOException JavaDoc e) {
82                 return false;
83             }
84         } catch (MalformedURLException JavaDoc e) {
85             throw new BuildException("Badly formed URL: " + spec, e);
86         }
87         return true;
88     }
89 }
90
Popular Tags