|
JMeter example source code file (HttpReplyHdr.java)
This example JMeter source code file (HttpReplyHdr.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The JMeter HttpReplyHdr.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.jmeter.protocol.http.proxy;
/**
* Utility class to generate HTTP responses of various types.
*
* @version $Revision: 915405 $
*/
public final class HttpReplyHdr {
/** String representing a carriage-return/line-feed pair. */
private static final String CR = "\r\n";
/** A HTTP protocol version string. */
private static final String HTTP_PROTOCOL = "HTTP/1.0";
/** The HTTP server name. */
private static final String HTTP_SERVER = "Java Proxy Server";
/**
* Don't allow instantiation of this utility class.
*/
private HttpReplyHdr() {
}
/**
* Forms a http ok reply header
*
* @param contentType
* the mime-type of the content
* @param contentLength
* the length of the content
* @return a string with the header in it
*/
public static String formOk(String contentType, long contentLength) {
StringBuilder out = new StringBuilder();
out.append(HTTP_PROTOCOL).append(" 200 Ok").append(CR);
out.append("Server: ").append(HTTP_SERVER).append(CR);
out.append("MIME-version: 1.0").append(CR);
if (0 < contentType.length()) {
out.append("Content-Type: ").append(contentType).append(CR);
} else {
out.append("Content-Type: text/html").append(CR);
}
if (0 != contentLength) {
out.append("Content-Length: ").append(contentLength).append(CR);
}
out.append(CR);
return out.toString();
}
/**
* private! builds an http document describing a headers reason.
*
* @param error
* Error name.
* @param description
* Errors description.
* @return A string with the HTML description body
*/
private static String formErrorBody(String error, String description) {
StringBuilder out = new StringBuilder();
// Generate Error Body
out.append("<HTML>");
out.append(error);
out.append("</TITLE>");
out.append("<BODY>").append(error).append("\n");
out.append("</P>");
out.append(description);
out.append("</BODY> |