alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

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.

Java - JMeter tags/keywords

access, content-type, gateway, method, method, mime-version, object, server, server, string, string, stringbuilder, stringbuilder, unathorized

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></HEAD>");
        out.append("<BODY><H2>").append(error).append("</H2>\n");
        out.append("</P></H3>");
        out.append(description);
        out.append("</BODY></HTML>");
        return out.toString();
    }

    /**
     * builds an http document describing an error.
     *
     * @param error
     *            Error name.
     * @param description
     *            Errors description.
     * @return A string with the HTML description body
     */
    private static String formError(String error, String description) {
        /*
         * A HTTP RESPONSE HEADER LOOKS ALOT LIKE:
         *
         * HTTP/1.0 200 OK Date: Wednesday, 02-Feb-94 23:04:12 GMT Server:
         * NCSA/1.1 MIME-version: 1.0 Last-modified: Monday, 15-Nov-93 23:33:16
         * GMT Content-Type: text/html Content-Length: 2345 \r\n
         */

        String body = formErrorBody(error, description);
        StringBuilder header = new StringBuilder();

        header.append(HTTP_PROTOCOL).append(" ").append(error).append(CR);
        header.append("Server: ").append(HTTP_SERVER).append(CR);
        header.append("MIME-version: 1.0").append(CR);
        header.append("Content-Type: text/html").append(CR);

        header.append("Content-Length: ").append(body.length()).append(CR);

        header.append(CR);
        header.append(body);

        return header.toString();
    }

    /**
     * Indicates a new file was created.
     *
     * @return The header in a string;
     */
    public static String formCreated() {
        return formError("201 Created", "Object was created");
    }

    /**
     * Indicates the document was accepted.
     *
     * @return The header in a string;
     */
    public static String formAccepted() {
        return formError("202 Accepted", "Object checked in");
    }

    /**
     * Indicates only a partial responce was sent.
     *
     * @return The header in a string;
     */
    public static String formPartial() {
        return formError("203 Partial", "Only partail document available");
    }

    /**
     * Indicates a requested URL has moved to a new address or name.
     *
     * @return The header in a string;
     */
    public static String formMoved() {
        // 300 codes tell client to do actions
        return formError("301 Moved", "File has moved");
    }

    /**
     * Never seen this used.
     *
     * @return The header in a string;
     */
    public static String formFound() {
        return formError("302 Found", "Object was found");
    }

    /**
     * The requested method is not implemented by the server.
     *
     * @return The header in a string;
     */
    public static String formMethod() {
        return formError("303 Method unseported", "Method unseported");
    }

    /**
     * Indicates remote copy of the requested object is current.
     *
     * @return The header in a string;
     */
    public static String formNotModified() {
        return formError("304 Not modified", "Use local copy");
    }

    /**
     * Client not otherized for the request.
     *
     * @return The header in a string;
     */
    public static String formUnautorized() {
        return formError("401 Unathorized", "Unathorized use of this service");
    }

    /**
     * Payment is required for service.
     *
     * @return The header in a string;
     */
    public static String formPaymentNeeded() {
        return formError("402 Payment required", "Payment is required");
    }

    /**
     * Client if forbidden to get the request service.
     *
     * @return The header in a string;
     */
    public static String formForbidden() {
        return formError("403 Forbidden", "You need permission for this service");
    }

    /**
     * The requested object was not found.
     *
     * @return The header in a string;
     */
    public static String formNotFound() {
        return formError("404 Not_found", "Requested object was not found");
    }

    /**
     * The server had a problem and could not fulfill the request.
     *
     * @return The header in a string;
     */
    public static String formInternalError() {
        return formError("500 Internal server error", "Server broke");
    }

    /**
     * Server does not do the requested feature.
     *
     * @return The header in a string;
     */
    public static String formNotImplemented() {
        return formError("501 Method not implemented", "Service not implemented");
    }

    /**
     * Server does not do the requested feature.
     *
     * @param reason detailed information for causing the failure
     * @return The header in a string;
     */
    public static String formNotImplemented(String reason) {
        return formError("501 Method not implemented", "Service not implemented. " + reason);
    }

    /**
     * Server is overloaded, client should try again latter.
     *
     * @return The header in a string;
     */
    public static String formOverloaded() {
        return formError("502 Server overloaded", "Try again latter");
    }

    /**
     * Indicates the request took to long.
     *
     * @return The header in a string;
     */
    public static String formTimeout() {
        return formError("503 Gateway timeout", "The connection timed out");
    }

    /**
     * Indicates the client's proxies could not locate a server.
     *
     * @return The header in a string;
     */
    public static String formServerNotFound() {
        return formError("503 Gateway timeout", "The requested server was not found");
    }

    /**
     * Indicates the client is not allowed to access the object.
     *
     * @return The header in a string;
     */
    public static String formNotAllowed() {
        return formError("403 Access Denied", "Access is not allowed");
    }
}
</pre>
<div id="after_source_code">
<h2>Other JMeter examples (source code examples)</h2>
<p>Here is a short list of links related to this JMeter HttpReplyHdr.java source code file:</p>
<ul>
  <li><a href="/java/jwarehouse"><img src="/images/scw/find.png" border="0"> The search page</a></li>
  <li><a href="index.shtml"><img src="/images/scw/folder.png" border="0"> Other JMeter source code examples at this package level</a></li>
  <li><a href="/java/jwarehouse/about.shtml"><img src="/images/scw/information.png" border="0"> Click here to learn more about this project</a></li>
</ul>
</div>
</td>
</tr>
</table>
</div>
</div>

<div style="padding-top: 1em; width: 310px; margin-left: auto; margin-right: auto; table {border-collapse: collapse; border: none;}; tr {border-collapse: collapse; border: none; text-align: center;};">
<table width="100%" cellspacing="0" cellpadding="0">
  <tr>
      <td colspan="2" style="border-collapse: collapse; border: none; text-align: center;};">
        <em>... this post is sponsored by my books ...</em>
      </td>
  </tr>
  <tr>
      <td width="150" style="border-collapse: collapse; border: none; text-align: center;};">
        <a href="https://kbhr.co/ckbk-v2"><img
           src="/images/books/scala-cookbook-v2-cover-220h.jpg"
           title="The Scala Cookbook, by Alvin Alexander" height="220" />
           <br /><span style="opacity: 0.4;">#1 New Release!</span></a>
      </td>
      <td width="150" style="border-collapse: collapse; border: none; text-align: center; padding-left: 8px;">
        <a href="http://kbhr.co/fps-book"><img
           src="/images/books/functional-programming-simplified-small.jpg"
           title="Functional Programming, Simplified, by Alvin Alexander"
           height="220" />
           <br /><span style="opacity: 0.4;">FP Best Seller</span></a>
      </td>
  </tr>
</table>
<p> </p>
</div>


<div id="whats_new">
<h2>new blog posts</h2>
<div id="whats_new_list">
<ul>
<li><a class="whats_new_link" href="/photos/shadow-cold-day-santa-fe">Shadow on a cold day in Santa Fe</a></li>
<li><a class="whats_new_link" href="/photos/appeal-functional-programming-scala">The appeal of functional programming (concurrency)</a></li>
<li><a class="whats_new_link" href="/photos/spanglish-inspired-sandwich">Spanglish-inspired sandwich</a></li>
<br/>
<li><a class="whats_new_link" href="/photos/higher-order-functions-are-haskell-experience">Higher order functions *are* the Haskell experience</a></li>
<li><a class="whats_new_link" href="/misc/my-life-extends-far-beyond-limitations-me">'''My life extends far beyond the limitations of me'''</a></li>
<li><a class="whats_new_link" href="/misc/awareness-admits-no-obstructions-boundless">Awareness admits no obstructions</a></li>
<br/>
<li><a class="whats_new_link" href="/personal/learning-empathy-through-dreams-touching-people-knowing-their-experiences-lives">Learning empathy through a dream (touching people and knowing their experiences)</a></li>
<li><a class="whats_new_link" href="/misc/each-instant-put-your-heart-into-it-again">Each instant, put your heart into it again</a></li>
<li><a class="whats_new_link" href="/misc/ive-been-seeing-a-therapist-dream-telekinesis-moving-furniture">I'''ve been seeing a therapist ...</a></li>
<br/>
<br/>
</div>
</ul>
</div>


<p> </p>

<p align="center"><font color="#000000" size="2"
face="Verdana,Arial">Copyright 1998-2024 Alvin Alexander, alvinalexander.com<br/>
All Rights Reserved.<br/>
<br/>
A percentage of advertising revenue from<br/>
pages under the <a href="/java/jwarehouse">/java/jwarehouse</a> 
URI on this website is<br/>
paid back to open source projects.</p>


<script>
shuffle(books);
var div = document.getElementById("leftcol");
var pre = '<div style="margin: 0; padding-right: 1.6em"><h2 align="center">favorite books</h2>';
var post = '</div>';
if (adblock) {
  var str = books.slice(0,3).join(" ");
  div.insertAdjacentHTML('beforeend', pre + str + post);
} else {
  var str = books.slice(0,1).join(" ");
  div.insertAdjacentHTML('beforeend', pre + str + post);
}
</script>

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3075717592179848"
        crossorigin="anonymous"></script>

<p style="padding-bottom: 80px;"> </p>


</body>