Package se.dannej.fakehttpserver.expect

Source Code of se.dannej.fakehttpserver.expect.ResponseActions

/*
* Copyright 2011 Daniel Josefsson
*
* 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 se.dannej.fakehttpserver.expect;

import java.io.IOException;

import org.apache.commons.io.IOUtils;

import se.dannej.fakehttpserver.FakeHttpServletRequest;
import se.dannej.fakehttpserver.FakeHttpServletResponse;
import se.dannej.fakehttpserver.FakeServerException;

/**
* Convenience class for creating {@link ResponseAction}s.
*/
public class ResponseActions {

  /**
   * Set status code
   * @param status
   * @return
   */
  public static ResponseAction sendStatus(final int status) {
    return new ResponseAction() {
      @Override
      public void applyTo(FakeHttpServletRequest request, FakeHttpServletResponse response) {
        response.setStatus(status);
      }
    };
  }

  /**
   * Set content-type
   * @param contentType
   * @return
   */
  public static ResponseAction sendContentType(final String contentType) {
    return new ResponseAction() {
      @Override
      public void applyTo(FakeHttpServletRequest request, FakeHttpServletResponse response) {
        response.setContentType(contentType);
      }
    };
  }

  /**
   * Set content
   * @param content
   * @return
   */
  public static ResponseAction sendContent(final String content) {
    return new ResponseAction() {
      @Override
      public void applyTo(FakeHttpServletRequest request, FakeHttpServletResponse response) {
        try {
          IOUtils.write(content, response.getOutputStream());
        } catch (IOException e) {
          throw new FakeServerException("Failed writing content to response", e);
        }
      }
    };
  }
 
  public static ResponseAction sendHeader(final String headerName, final String headerValue) {
    return new ResponseAction() {
      @Override
      public void applyTo(FakeHttpServletRequest request, FakeHttpServletResponse response) {
        response.addHeader(headerName, headerValue);
      }
    };
  }
}
TOP

Related Classes of se.dannej.fakehttpserver.expect.ResponseActions

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.