/*
 * 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.Date;
import java.util.Set;

public final class ModifiedSinceHandler extends PrefixedHandler {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(ModifiedSinceHandler.class);
    private final String modifyDate = new Date().toString();

    public ModifiedSinceHandler(final Set<String> pathPrefix) {
        super(pathPrefix);
    }

    @Override
    public HandlerStatus handlePrefixedWebRequest(final HttpTransaction request) throws Throwable {
        if (request.httpServletRequest.getHeader("If-Modified-Since") != null) {
            request.httpServletResponse.setStatus(304);
            logger.debug("Indicating 304");
            return HandlerStatus.BREAK;
        }

        request.httpServletResponse.setHeader("Last-Modified", modifyDate);

        logger.debug("Indicating CONTINUE");
        return HandlerStatus.CONTINUE;
    }
}
