source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/condition/Http.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 2.9 KB
Line 
1/*
2 * Copyright 2001-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs.condition;
19
20import java.net.HttpURLConnection;
21import java.net.MalformedURLException;
22import java.net.URL;
23import java.net.URLConnection;
24import org.apache.tools.ant.BuildException;
25import org.apache.tools.ant.Project;
26import org.apache.tools.ant.ProjectComponent;
27
28/**
29 * Condition to wait for a HTTP request to succeed. Its attribute(s) are:
30 * url - the URL of the request.
31 * errorsBeginAt - number at which errors begin at; default=400.
32 * @since Ant 1.5
33 */
34public class Http extends ProjectComponent implements Condition {
35 private String spec = null;
36
37 /**
38 * Set the url attribute
39 *
40 * @param url the url of the request
41 */
42 public void setUrl(String url) {
43 spec = url;
44 }
45
46 private int errorsBeginAt = 400;
47
48 /**
49 * Set the errorsBeginAt attribute
50 *
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 url = new URL(spec);
69 try {
70 URLConnection conn = url.openConnection();
71 if (conn instanceof HttpURLConnection) {
72 HttpURLConnection http = (HttpURLConnection) 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 } else {
79 return false;
80 }
81 }
82 } catch (java.io.IOException e) {
83 return false;
84 }
85 } catch (MalformedURLException e) {
86 throw new BuildException("Badly formed URL: " + spec, e);
87 }
88 return true;
89 }
90}
Note: See TracBrowser for help on using the repository browser.