| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.util.http; |
| 16 |
|
| 17 |
public class HTTPProtocol { |
| 18 |
|
| 19 |
public static final String CRLF = "\r\n"; |
| 20 |
public static final String HTTP_VERSION = "HTTP/1.1"; |
| 21 |
public static final String METHOD = "POST "; |
| 22 |
public static final String HOST = "Host: "; |
| 23 |
public static final String CONTENT_LENGTH = "Content-Length: "; |
| 24 |
public static final String CONTENT_TYPE = "Content-Type: "; |
| 25 |
public static final String USER_AGENT = "User-Agent: "; |
| 26 |
public static final String SERVER = "Server: "; |
| 27 |
public static final String CONNECTION = "Connection: "; |
| 28 |
public static final String LOCATION = "Location: "; |
| 29 |
public static final String CHUNKED = "Transfer-Encoding: chunked"; |
| 30 |
|
| 31 |
public static final String CONNECTION_CLOSE = "Connection: close\r\n"; |
| 32 |
public static final String CHUNKING = CHUNKED + CRLF; |
| 33 |
|
| 34 |
|
| 35 |
/* Used for GRAM and GARA messages */ |
| 36 |
protected static String createHTTPHeader(String service, |
| 37 |
String hostname, |
| 38 |
String application, |
| 39 |
StringBuffer msg) { |
| 40 |
|
| 41 |
StringBuffer head = new StringBuffer(); |
| 42 |
head.append("POST ").append(service).append(" ").append(HTTP_VERSION).append(CRLF); |
| 43 |
head.append(HOST).append(hostname).append(CRLF); |
| 44 |
head.append(CONTENT_TYPE).append(application).append(CRLF); |
| 45 |
head.append(CONTENT_LENGTH).append(String.valueOf(msg.length())).append(CRLF); |
| 46 |
head.append(CRLF); |
| 47 |
head.append(msg); |
| 48 |
|
| 49 |
return head.toString(); |
| 50 |
} |
| 51 |
|
| 52 |
/* Used for GASS GET */ |
| 53 |
public static String createGETHeader(String path, |
| 54 |
String host, |
| 55 |
String user_agent) { |
| 56 |
|
| 57 |
StringBuffer head = new StringBuffer(); |
| 58 |
head.append("GET " + path + " " + HTTP_VERSION + CRLF); |
| 59 |
head.append(HOST + host + CRLF); |
| 60 |
head.append(CONNECTION_CLOSE); |
| 61 |
head.append(USER_AGENT + user_agent + CRLF); |
| 62 |
head.append(CRLF); |
| 63 |
|
| 64 |
return head.toString(); |
| 65 |
} |
| 66 |
|
| 67 |
/* Used for GASS PUT */ |
| 68 |
public static String createPUTHeader(String path, |
| 69 |
String host, |
| 70 |
String user_agent, |
| 71 |
String type, |
| 72 |
long length, |
| 73 |
boolean append) { |
| 74 |
|
| 75 |
StringBuffer head = new StringBuffer(); |
| 76 |
|
| 77 |
if (append) { |
| 78 |
head.append("POST "); |
| 79 |
} else { |
| 80 |
head.append("PUT "); |
| 81 |
} |
| 82 |
|
| 83 |
head.append(path + " " + HTTP_VERSION + CRLF); |
| 84 |
|
| 85 |
head.append(HOST + host + CRLF); |
| 86 |
head.append(CONNECTION_CLOSE); |
| 87 |
head.append(USER_AGENT + user_agent + CRLF); |
| 88 |
head.append(CONTENT_TYPE + type + CRLF); |
| 89 |
|
| 90 |
if (length == -1) { |
| 91 |
head.append(CHUNKING); |
| 92 |
} else { |
| 93 |
head.append(CONTENT_LENGTH + length + CRLF); |
| 94 |
} |
| 95 |
|
| 96 |
head.append(CRLF); |
| 97 |
|
| 98 |
return head.toString(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
// ------ new HTTP stuff --------------------------- |
| 103 |
|
| 104 |
public static String ErrorReply(int error, String msg) { |
| 105 |
return getErrorReply(error, msg); |
| 106 |
} |
| 107 |
|
| 108 |
public static String getErrorReply(int error, String message) { |
| 109 |
StringBuffer head = new StringBuffer(); |
| 110 |
|
| 111 |
head.append(HTTP_VERSION) |
| 112 |
.append(" ") |
| 113 |
.append(String.valueOf(error)) |
| 114 |
.append(" ") |
| 115 |
.append(message) |
| 116 |
.append(CRLF) |
| 117 |
.append(CONNECTION_CLOSE) |
| 118 |
.append(CRLF); |
| 119 |
|
| 120 |
return head.toString(); |
| 121 |
} |
| 122 |
|
| 123 |
public static String getBadRequestErrorReply() { |
| 124 |
return getErrorReply(400, "BAD REQUEST"); |
| 125 |
} |
| 126 |
|
| 127 |
public static String getFileNotFoundErrorReply() { |
| 128 |
return getErrorReply(404, "FILE NOT FOUND"); |
| 129 |
} |
| 130 |
|
| 131 |
public static String getServerErrorReply() { |
| 132 |
return getErrorReply(500, "INTERAL SERVER ERROR"); |
| 133 |
} |
| 134 |
|
| 135 |
public static String getForbiddenErrorReply() { |
| 136 |
return getErrorReply(403, "FORBIDDEN"); |
| 137 |
} |
| 138 |
|
| 139 |
public static String getOKReply(String application) { |
| 140 |
return getOKReply(application, null); |
| 141 |
} |
| 142 |
|
| 143 |
public static String getOKReply(String application, String msg) { |
| 144 |
StringBuffer head = new StringBuffer(); |
| 145 |
|
| 146 |
head.append(HTTP_VERSION) |
| 147 |
.append(" 200 OK") |
| 148 |
.append(CRLF) |
| 149 |
.append(CONTENT_TYPE) |
| 150 |
.append(application) |
| 151 |
.append(CRLF) |
| 152 |
.append(CONTENT_LENGTH); |
| 153 |
|
| 154 |
if (msg == null) { |
| 155 |
head.append("0"); |
| 156 |
} else { |
| 157 |
head.append(msg.length()); |
| 158 |
} |
| 159 |
|
| 160 |
head.append(CRLF).append(CRLF); |
| 161 |
|
| 162 |
if (msg != null) { |
| 163 |
head.append(msg); |
| 164 |
} |
| 165 |
|
| 166 |
return head.toString(); |
| 167 |
} |
| 168 |
} |