001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.request.http.handler; 018 019import org.apache.wicket.request.IRequestCycle; 020import org.apache.wicket.request.IRequestHandler; 021import org.apache.wicket.request.http.WebResponse; 022 023 024/** 025 * Response target that is to be used in a servlet environment to send an error code and optionally 026 * a message. NOTE: this target can only be used in a servlet environment with 027 * {@link IRequestCycle}s. 028 * 029 * @author Eelco Hillenius 030 */ 031public final class ErrorCodeRequestHandler implements IRequestHandler 032{ 033 /** the servlet error code. */ 034 private final int errorCode; 035 036 /** the optional message to send to the client. */ 037 private final String message; 038 039 /** 040 * Construct. 041 * 042 * @param errorCode 043 * the servlet error code; use one of the 044 * {@link javax.servlet.http.HttpServletResponse} constants 045 * @see javax.servlet.http.HttpServletResponse 046 */ 047 public ErrorCodeRequestHandler(final int errorCode) 048 { 049 this(errorCode, null); 050 } 051 052 /** 053 * Construct. 054 * 055 * @param errorCode 056 * the servlet error code; use one of the 057 * {@link javax.servlet.http.HttpServletResponse} constants 058 * @param message 059 * the optional message to send to the client 060 * @see javax.servlet.http.HttpServletResponse 061 */ 062 public ErrorCodeRequestHandler(final int errorCode, final String message) 063 { 064 this.errorCode = errorCode; 065 this.message = message; 066 } 067 068 /** 069 * Respond by sending the set errorCode and optionally the message to the browser. 070 * 071 * @see org.apache.wicket.request.IRequestHandler#respond(org.apache.wicket.request.IRequestCycle) 072 */ 073 @Override 074 public void respond(final IRequestCycle requestCycle) 075 { 076 WebResponse webResponse = (WebResponse)requestCycle.getResponse(); 077 webResponse.sendError(errorCode, message); 078 } 079 080 /** 081 * Gets the servlet error code. 082 * 083 * @return the servlet error code 084 */ 085 public final int getErrorCode() 086 { 087 return errorCode; 088 } 089 090 /** 091 * Gets the optional message to send to the client. 092 * 093 * @return the optional message to send to the client 094 */ 095 public final String getMessage() 096 { 097 return message; 098 } 099}