001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package core.tut.pori.http;
017
018import java.io.IOException;
019
020import javax.servlet.http.HttpServletResponse;
021
022import org.apache.log4j.Logger;
023
024/**
025 * Simple Response class for sending a redirect to a provided URL
026 * 
027 */
028public class RedirectResponse extends Response{
029  private static final Logger LOGGER = Logger.getLogger(RedirectResponse.class);
030  private String _redirectUrl = null;
031
032  @Override
033  public void writeTo(HttpServletResponse response) {
034    try {
035      if(_redirectUrl == null){
036        LOGGER.debug("No redirect URL given, will not redirect...");
037        super.writeTo(response);
038      }else{
039        response.sendRedirect(_redirectUrl);
040      }
041    } catch (IOException ex) {
042      LOGGER.error(ex, ex);
043      setStatus(Status.INTERNAL_SERVER_ERROR);
044      super.writeTo(response);
045    }
046  }
047  
048  /**
049   * for serialization
050   */
051  public RedirectResponse(){
052    // nothing needed
053  }
054  
055  /**
056   * 
057   * @param url where to redirect
058   */
059  public RedirectResponse(String url){
060    _redirectUrl = url;
061  }
062}