/*
 * This file is part of DebGen Biobank Software developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 * Copyright ASTRID Research Inc., Hungary, 2009-2011.
 *
 * ASTRID Research
 * Headquaters: Csapo utca 42. Debrecen H-4029 HUNGARY
 * Phone: +36-52-533-173        Fax: +36-52-533-174
 * Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DebGen Biobank Software project.
 * URLS:
 * Developers: Istvan Zoltan Nagy (istvan.nagy@astrid.hu)
 */
package hu.astrid.debgen.server.requestprocessor.service;

import hu.astrid.debgen.server.requestprocessor.*;
import hu.astrid.debgen.authentication.domain.DebGenUser;
import hu.astrid.debgen.server.requestprocessor.exception.InvalidPageNameFormatException;
import hu.astrid.debgen.server.startup.ApplicationParameters;
import javax.activity.InvalidActivityException;
import javax.servlet.http.HttpServletRequest;

public class RequestProcessorService {

    /**Returns an ActionParameter object from the given uri
     * @param uri The page which was requested
     * @return The ActionParameter
     */
    public static ActionParameter getActionParameterFromUri(String uri) throws InvalidPageNameFormatException, Exception {
        String appURIName = ApplicationParameters.getApplicationURI();
        try {
            int from = appURIName.length() + 2;
            int to = uri.indexOf(".");
            if (from < 0 || to < 0 || from >= to) {
                return ActionParameter.getInvalidPageActionParameter(new InvalidActivityException(uri));
            } else {
                return new ActionParameter(uri.substring(from, to));
            }
        } catch (Exception ex) {
            throw ex;
//            return ActionParameter.mainPage();
        }
    }

    /**
     * Returns the current user from the session
     * @param request The current request
     * @return The current User
     */
    public static DebGenUser getDebGenUser(HttpServletRequest request) {
        return (DebGenUser) request.getSession().getAttribute("user");
    }
}
