/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */
package org.apache.directory.fortress.rest;

import javax.servlet.http.HttpServletRequest;

import org.apache.directory.fortress.core.GlobalErrIds;
import org.apache.directory.fortress.core.SecurityException;
import org.apache.directory.fortress.core.model.FortRequest;
import org.apache.directory.fortress.core.model.FortResponse;
import org.apache.directory.fortress.core.model.Session;
import org.apache.directory.fortress.core.util.Config;
import org.apache.directory.fortress.realm.J2eePolicyMgr;
import org.apache.directory.fortress.realm.J2eePolicyMgrFactory;
import org.apache.log4j.Logger;


/**
 * Grab the Apache Fortress (RBAC) session from Tomcat container via the HttpServletRequest interface. This class is thread safe.
 *
 */
public class SecUtils
{
    private static final Logger LOG = Logger.getLogger(SecUtils.class.getName());
    private static J2eePolicyMgr j2eePolicyMgr;

    static
    {
        try
        {
            j2eePolicyMgr = J2eePolicyMgrFactory.createInstance();
        }
        catch (SecurityException se)
        {
            String error = "initializeSession caught SecurityException in static block=" + se.getMessage();
            LOG.warn( error );
        }
    }

    /**
     * Use Apache Fortress Realm interface to load the RBAC session via a standard interface.
     *
     * @param fortRequest Used to carry the session and other data.
     * @param httpRequest Used to get the security principal.
     * @return Response will contain the RBAC session object (if found) or a system error if a problem in the get.  If arbac02 isn't enabled, it will return a NULL.
     */
    static FortResponse initializeSession(FortRequest fortRequest, HttpServletRequest httpRequest)
    {
        Session realmSession;
        FortResponse fortResponse = null;
        // Have the fortress arbac02 runtime checks been enabled?.
        if (Config.getInstance().getBoolean("is.arbac02"))
        {
            if (httpRequest == null)
            {
                // Improper container config.
                fortResponse = new FortResponse();
                fortResponse.setErrorCode(GlobalErrIds.REST_NULL_HTTP_REQ_ERR);
                fortResponse.setErrorMessage("initializeSession detected null HTTP Request");
                fortResponse.setHttpStatus(403);
            }
            else
            {
                try
                {
                    // Get the security principal from the runtime.
                    String szPrincipal = httpRequest.getUserPrincipal().toString();
                    // This has to happen before it can be used by Fortress.
                    realmSession = j2eePolicyMgr.deserialize(szPrincipal);
                    if (realmSession != null)
                    {
                        // The RBAC Session successfully grabbed from the container.
                        fortRequest.setSession(realmSession);
                    }
                    else
                    {
                        String error = "initializeSession couldn't get a Security Session.";
                        fortResponse = new FortResponse();
                        fortResponse.setErrorCode(GlobalErrIds.USER_SESS_NULL);
                        fortResponse.setErrorMessage(error);
                        fortResponse.setHttpStatus(403);
                        LOG.info(error);
                    }
                }
                catch (SecurityException se)
                {
                    // A problem deserializing the security principal.
                    String error = "initializeSession caught SecurityException=" + se.getMessage();
                    fortResponse = new FortResponse();
                    LOG.info(error);
                    fortResponse.setErrorCode(se.getErrorId());
                    fortResponse.setErrorMessage(error);
                    fortResponse.setHttpStatus(se.getHttpStatus());
                }
            }
        }
        return fortResponse;
    }
}
@@ -2905,245 +2905,106 @@
 e = 
-new FortResponse();%0A                fortResponse.setErrorCode(GlobalErrIds.REST_NULL_HTTP_REQ_ERR);%0A                fortResponse.setErrorMessage(%22initializeSession detected null HTTP Request%22);%0A                fortResponse.setHttpStatus(
+createError( GlobalErrIds.REST_NULL_HTTP_REQ_ERR, %22initializeSession detected null HTTP Request%22, 
 403)
@@ -3704,372 +3704,122 @@
     
-String error = %22initializeSession couldn't get a Security Session.%22;%0A                        fortResponse = new FortResponse();%0A                        fortResponse.setErrorCode(GlobalErrIds.USER_SESS_NULL);%0A                        fortResponse.setErrorMessage(error);%0A                        fortResponse.setHttpStatus(403);%0A                        LOG.info(error
+fortResponse = createError( GlobalErrIds.USER_SESS_NULL, %22initializeSession couldn't get a Security Session.%22, 403
 );%0A 
@@ -4007,38 +4007,68 @@
             
-String error =
+fortResponse = createError( se.getErrorId(),
  %22initialize
@@ -4119,38 +4119,89 @@
 essage()
-;%0A            
+, se.getHttpStatus());%0A                %7D%0A            %7D%0A        %7D%0A
         
 fortResp
@@ -4184,32 +4184,39 @@
       %7D%0A        
+return 
 fortResponse = n
@@ -4215,48 +4215,112 @@
 onse
- = new FortResponse();%0A     
+;%0A    %7D%0A%0A    private static FortResponse createError(int errId, String errMsg, int hCode)%0A
     
+%7B%0A
         
    L
@@ -4319,40 +4319,56 @@
     
-   LOG.info(error);%0A            
+FortResponse fortResponse = new FortResponse();%0A
     
@@ -4401,38 +4401,16 @@
 ode(
-se.getErro
+er
 rId
-()
 );%0A
-            
     
@@ -4449,25 +4449,14 @@
 (err
-or);%0A            
+Msg);%0A
     
@@ -4490,70 +4490,41 @@
 tus(
-se.getHttpStatus());%0A                %7D%0A            %7D%0A        %7D
+hCode);%0A        LOG.info(errMsg);
 %0A   

add a switch to turn on / off arbac02 checks.