001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.html.servlets;
023
024import java.io.IOException;
025
026import javax.servlet.http.Cookie;
027import javax.servlet.http.HttpServlet;
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030
031public final class NegotiationSelector extends HttpServlet {
032
033    private static final long serialVersionUID = 1L;
034
035    @Override
036    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
037        doPost(request, response);
038    }
039
040    @Override
041    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
042
043        final String referrer = request.getHeader("referer");
044        if (referrer == null) {
045            response.getWriter().write("no referrer found");
046            return;
047        }
048
049        final String engine = request.getParameter("engine");
050        if (engine != null) {
051            response.addCookie(createCookie("nxthemes.engine", engine));
052        }
053
054        final String mode = request.getParameter("mode");
055        if (mode != null) {
056            response.addCookie(createCookie("nxthemes.mode", mode));
057        }
058
059        final String theme = request.getParameter("theme");
060        if (theme != null) {
061            response.addCookie(createCookie("nxthemes.theme", theme));
062        }
063
064        final String perspective = request.getParameter("perspective");
065        if (perspective != null) {
066            response.addCookie(createCookie("nxthemes.perspective", perspective));
067        }
068
069        response.sendRedirect(referrer);
070    }
071
072    private Cookie createCookie(final String name, final String value) {
073        final Cookie cookie = new Cookie(name, value);
074
075        // remove the cookie of the value is an empty string
076        if (value.equals("")) {
077            cookie.setMaxAge(0);
078        }
079        cookie.setPath("/");
080        return cookie;
081    }
082}