de.micromata.genome.tpsb.httpmockup.MockHttpServletResponse Maven / Gradle / Ivy
The newest version!
//
// Copyright (C) 2010-2016 Micromata GmbH
//
// Licensed 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 de.micromata.genome.tpsb.httpmockup;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.CharEncoding;
import org.apache.log4j.Logger;
/**
* Mockup servlet response.
*
* Adopted from stripes.
*
* @author Roger Rene Kommer ([email protected])
*
*/
public class MockHttpServletResponse implements HttpServletResponse
{
private static Logger log = Logger.getLogger(MockHttpServletResponse.class);
private MockServletOutputStream out = new MockServletOutputStream();
private PrintWriter writer = new PrintWriter(out, true);
private Locale locale = Locale.getDefault();
private Map> headers = new HashMap>();
private List cookies = new ArrayList();
private int status = 200;
private String errorMessage;
private String characterEncoding = CharEncoding.UTF_8;
private int contentLength;
private String contentType = "text/html";
private String redirectUrl;
private String getStandardStatusMessage()
{
if (errorMessage != null) {
return errorMessage;
}
switch (status) {
case 200:
return "OK";
case 302:
return "Moved Temporarly";
default:
return "OK";
}
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("HTTP/1.1 ").append(status).append(" ").append(getStandardStatusMessage()).append("\n");
for (Map.Entry> me : headers.entrySet()) {
for (Object o : me.getValue()) {
sb.append(me.getKey()).append(": ").append(o).append("\n");
}
}
sb.append("\n");
sb.append(getOutputString());
return sb.toString();
}
/** Adds a cookie to the set of cookies in the response. */
@Override
public void addCookie(Cookie cookie)
{
this.cookies.add(cookie);
}
/** Gets the set of cookies stored in the response. */
public Cookie[] getCookies()
{
return this.cookies.toArray(new Cookie[this.cookies.size()]);
}
/** Returns true if the specified header was placed in the response. */
@Override
public boolean containsHeader(String name)
{
return this.headers.containsKey(name);
}
/** Returns the URL unchanged. */
@Override
public String encodeURL(String url)
{
return url;
}
/** Returns the URL unchanged. */
@Override
public String encodeRedirectURL(String url)
{
return url;
}
/** Returns the URL unchanged. */
@Override
public String encodeUrl(String url)
{
return url;
}
/** Returns the URL unchanged. */
@Override
public String encodeRedirectUrl(String url)
{
return url;
}
/** Sets the status code and saves the message so it can be retrieved later. */
@Override
public void sendError(int status, String errorMessage) throws IOException
{
this.status = status;
this.errorMessage = errorMessage;
}
/** Sets that status code to the error code provided. */
@Override
public void sendError(int status) throws IOException
{
this.status = status;
}
/** Simply stores the URL that was supplied, so that it can be examined later with getRedirectUrl. */
@Override
public void sendRedirect(String url) throws IOException
{
log.debug("sendRedirect: " + url);
this.redirectUrl = url;
this.status = 302;
addHeader("Location", url);
}
/**
* If a call was made to sendRedirect() this method will return the URL that was supplied. Otherwise it will return
* null.
*/
public String getRedirectUrl()
{
return this.redirectUrl;
}
/** Stores the value in a Long and saves it as a header. */
@Override
public void setDateHeader(String name, long value)
{
this.headers.remove(name);
addDateHeader(name, value);
}
/** Adds the specified value for the named header (does not remove/replace existing values). */
@Override
public void addDateHeader(String name, long value)
{
List