001/* 002 * ModeShape (http://www.modeshape.org) 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 org.modeshape.web; 017 018import java.io.IOException; 019import javax.servlet.ServletException; 020import javax.servlet.http.HttpServlet; 021import javax.servlet.http.HttpServletRequest; 022import javax.servlet.http.HttpServletResponse; 023import org.modeshape.common.logging.Logger; 024 025/** 026 * @author kulikov 027 */ 028public class InitialServlet extends HttpServlet { 029 030 private static final long serialVersionUID = 1L; 031 private Logger logger = Logger.getLogger(InitialServlet.class); 032 033 /** 034 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. 035 * 036 * @param request servlet request 037 * @param response servlet response 038 * @throws ServletException if a servlet-specific error occurs 039 * @throws IOException if an I/O error occurs 040 */ 041 protected void processRequest( HttpServletRequest request, 042 HttpServletResponse response ) throws ServletException, IOException { 043 String authHeader = ((HttpServletRequest)request).getHeader("Authorization"); 044 String pathInfo= request.getPathInfo(); 045 046 Object marker = request.getSession().getAttribute("login-required-marker"); 047 048 if (marker == null && pathInfo.equals("login.do")) { 049 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 050 response.addHeader("WWW-Authenticate", "Basic realm =\"Realm\""); 051 request.getSession().setAttribute("login-required-marker", Boolean.FALSE); 052 return; 053 } 054 055 if (pathInfo.equals("login.do")) { 056 response.sendRedirect(request.getContextPath() + "/Console.html"); 057 return; 058 } 059 060 String url = request.getRequestURI(); 061 String servletPath = request.getServletPath(); 062 063 request.getSession(true).setAttribute("initial.uri", url); 064 logger.debug("Store requested uri " + url); 065 066 String dest = url.substring(0, url.indexOf(servletPath)); 067 response.sendRedirect(dest); 068 } 069 070 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 071 /** 072 * Handles the HTTP <code>GET</code> method. 073 * 074 * @param request servlet request 075 * @param response servlet response 076 * @throws ServletException if a servlet-specific error occurs 077 * @throws IOException if an I/O error occurs 078 */ 079 @Override 080 protected void doGet( HttpServletRequest request, 081 HttpServletResponse response ) throws ServletException, IOException { 082 processRequest(request, response); 083 } 084 085 /** 086 * Handles the HTTP <code>POST</code> method. 087 * 088 * @param request servlet request 089 * @param response servlet response 090 * @throws ServletException if a servlet-specific error occurs 091 * @throws IOException if an I/O error occurs 092 */ 093 @Override 094 protected void doPost( HttpServletRequest request, 095 HttpServletResponse response ) throws ServletException, IOException { 096 processRequest(request, response); 097 } 098 099 /** 100 * Returns a short description of the servlet. 101 * 102 * @return a String containing servlet description 103 */ 104 @Override 105 public String getServletInfo() { 106 return "Short description"; 107 }// </editor-fold> 108}