/*
 * 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;

import hu.astrid.debgen.authentication.domain.DebGenUser;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * Put request's information and parameters into a string.
 * @author inagy
 */
public class RequestTokenizer {

    public static String getRequestData(HttpServletRequest req) {
        try {
            StringBuilder sb = new StringBuilder();
            try {
                DebGenUser user = (DebGenUser) req.getSession().getAttribute("user");
                sb.append("[Logged user: " + user.getUserName() + "]");
            } catch (Exception ex) {
                //IGNORED
                sb.append("[No logged user found.]");
            }
            sb.append("[Query string:" + req.getQueryString() + "]");
            sb.append("[Remote address: " + req.getRemoteAddr() + "]");
            sb.append("[Remote host:port: " + req.getRemoteHost() + ":" + req.getRemotePort() + "]");
            sb.append("[Character encoding:" + req.getCharacterEncoding() + "]");
            Enumeration<String> enumer = req.getParameterNames();
            while (enumer.hasMoreElements()) {
                String key = (String) enumer.nextElement();
                if (key.equals("password") == false) {
                    sb.append("[" + key + "=" + req.getParameter(key) + "]");
                }
            }
            return sb.toString();
        } catch (Throwable ex) {
            return "[Error during tokenizing request + " + ex.getMessage() + "]";
        }
    }
}
