/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package lc.mecha.http.server;

import lc.mecha.fabric.HandlerStatus;
import lc.mecha.log.MechaLogger;
import lc.mecha.log.MechaLoggerFactory;

import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: aleigh
 * Date: 5/22/14
 * Time: 12:24 AM
 * To change this template use File | Settings | File Templates.
 */
public class RedirectHandler extends PathHandler {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(RedirectHandler.class);

    private final String target;

    public RedirectHandler(final Set<String> pathPrefix, final String target) {
        super(pathPrefix);
        this.target = target;
    }

    @Override
    public HandlerStatus handlePrefixedWebRequest(final HttpTransaction request) throws Throwable {
        logger.debug("Redirecting request: {}", request);
        request.httpServletResponse.setHeader("Location", target);
        request.httpServletResponse.setStatus(302);
        return HandlerStatus.BREAK;
    }
}
