/*
 * Copyright (C) 2004-2007  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.pattern;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.exedio.cope.Item;

/**
 * Specifies a http redirect (moved permanently) to
 * a {@link Media}.
 * <p>
 * Common usage is to maintain old urls after renaming a {@link Media}.
 * For instance, if there is a media <tt>picture</tt>:
 *
 * <pre>
 * static final Media picture = new Media(OPTIONAL);
 * </pre>
 * and this media is renamed to <tt>image</tt>:
 *
 * <pre>
 * static final Media image = new Media(OPTIONAL);
 * </pre>
 * then old urls created by <tt>picture</tt>
 * can be supported with an additional:
 *
 * <pre>
 * static final MediaRedirect picture = new MediaRedirect(image);
 * </pre>
 *
 * @author Ralf Wiebicke
 */
public final class MediaRedirect extends MediaPath
{
	private final Media target;

	public MediaRedirect(final Media target)
	{
		if(target==null)
			throw new NullPointerException("target must not be null");

		this.target = target;
	}
	
	public final Media getTarget()
	{
		return target;
	}
	
	@Override
	public String getContentType(final Item item)
	{
		return target.getContentType(item);
	}

	private static final String RESPONSE_LOCATION = "Location";
	
	@Override
	public final Media.Log doGet(
			final HttpServletRequest request, final HttpServletResponse response,
			final Item item, final String extension)
		throws ServletException, IOException
	{
		final String url = target.getURL(item);
		if(url==null)
			return isNull;
		
		final String location =
			request.getScheme() + "://" +
			request.getHeader("Host") +
			request.getContextPath() + '/' +
			url;
		//System.out.println("location="+location);
		
		response.setStatus(response.SC_MOVED_PERMANENTLY);
		response.setHeader(RESPONSE_LOCATION, location);
		return delivered;
	}

}

@@ -2390,16 +2390,22 @@
 l String
+Buffer
  locatio
@@ -2411,45 +2411,368 @@
 on =
-%0A%09%09%09request.getScheme() + 
+ new StringBuffer();%0A%09%09%0A%09%09if(url.startsWith(%22http:%22)%7C%7Curl.startsWith(%22https:%22))%0A%09%09%7B%0A%09%09%09location.append(url);%0A%09%09%7D%0A%09%09else if(url.startsWith(%22/%22))%0A%09%09%7B%0A%09%09%09location.%0A%09%09%09%09append(request.getScheme()).%0A%09%09%09%09append(%22://%22).%0A%09%09%09%09append(request.getHeader(%22Host%22)).%0A%09%09%09%09append(url);%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09location.%0A%09%09%09%09append(request.getScheme()).%0A%09%09%09%09append(
 %22://%22
- +
+).
 %0A%09%09%09
+%09append(
 requ
@@ -2792,22 +2792,30 @@
 (%22Host%22)
- +%0A%09%09%09
+).%0A%09%09%09%09append(
 request.
@@ -2834,25 +2834,49 @@
 th()
- + '/' +%0A%09%09%09
+).%0A%09%09%09%09append('/').%0A%09%09%09%09append(
 url
+)
 ;%0A
+%09%09%7D%0A
 %09%09//
@@ -3017,24 +3017,35 @@
 ON, location
+.toString()
 );%0A%09%09return 
@@ -3055,14 +3055,13 @@
 ivered;%0A
+
 %09%7D%0A
-%0A
 %7D%0A

fix for absolute media root urls from r8053