001: package com.meterware.pseudoserver;
002:
003: /********************************************************************************************************************
004: * $Id: PseudoServlet.java,v 1.5 2003/09/30 00:15:05 russgold Exp $
005: *
006: * Copyright (c) 2000-2002, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.io.Reader;
024: import java.io.IOException;
025:
026: /**
027: * A basic simulated servlet for testing the HttpUnit library.
028: **/
029: abstract public class PseudoServlet {
030:
031: final static public String CONTENTS = "contents";
032:
033: /**
034: * Returns a resource object as a result of a get request.
035: **/
036: public WebResource getResponse(String methodType)
037: throws IOException {
038: if (methodType.equalsIgnoreCase("GET")) {
039: return getGetResponse();
040: } else if (methodType.equalsIgnoreCase("PUT")) {
041: return getPutResponse();
042: } else if (methodType.equalsIgnoreCase("POST")) {
043: return getPostResponse();
044: } else {
045: throw new UnknownMethodException(methodType);
046: }
047: }
048:
049: /**
050: * Returns a resource object as a result of a get request.
051: **/
052: public WebResource getGetResponse() throws IOException {
053: throw new UnknownMethodException("GET");
054: }
055:
056: /*
057: * Returns a resource object as a result of a post request.
058: **/
059: public WebResource getPostResponse() throws IOException {
060: throw new UnknownMethodException("POST");
061: }
062:
063: /*
064: * Returns a resource object as a result of a put request.
065: **/
066: public WebResource getPutResponse() throws IOException {
067: throw new UnknownMethodException("PUT");
068: }
069:
070: void init(HttpRequest requestStream) {
071: _request = requestStream;
072: }
073:
074: /**
075: * Returns the header with the specified name. If no such header exists, will return null.
076: **/
077: protected String getHeader(String name) {
078: return _request.getHeader(name);
079: }
080:
081: /**
082: * Returns the values for the parameter with the specified name. If no values exist
083: * will return null.
084: **/
085: protected String[] getParameter(String name) {
086: return _request.getParameter(name);
087: }
088:
089: /**
090: * Returns a reader for the body of the request.
091: **/
092: protected Reader getReader() {
093: return _request.getReader();
094: }
095:
096: protected byte[] getBody() {
097: return _request.getBody();
098: }
099:
100: protected HttpRequest getRequest() {
101: return _request;
102: }
103:
104: private HttpRequest _request;
105:
106: }
|