Package HTTP

Source Code of HTTP.HTTPutils

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package HTTP;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import Framework.Array_Of_NamedElement;
import Framework.NamedElement;
import Framework.TextData;
import HTTPSupport.Array_Of_Cookie;

/**
* This is a utility class to provide translation from the Forte Request and Response Classes,
* to the Java HttpServlet request and response classes.
*
*/
public class HTTPutils {
    private static Logger _log = Logger.getLogger(HTTPutils.class);
    private HTTPutils() {
        super();
    }
    public static String getTemplateName(HttpServletRequest request){
        String name = null;
        name = HTMLScanner.getParameter(request, "TemplateName", false);
        return name;
    }
    public static TextData findNameCookie(javax.servlet.http.HttpServletRequest request, String name){

        Cookie cookie = HTTPutils.findCookie(request, name);
        if (cookie == null) {
            return null;
        } else {
            return new TextData(cookie.getValue());
        }
    }

    public static Cookie findCookie(javax.servlet.http.HttpServletRequest request, String name){
        _log.debug("Looking for named cookie: " + name);
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {

            for (int i = 0; i < cookies.length; i++) {
                if (name.equals(cookies[i].getName())) {
                    _log.debug("Found named cookie: " + name);
                    return cookies[i];
                }
            }
        }
        _log.debug("Cannot find named cookie: " + name);
        return null;
    }

    /**
     * This method returns all the cookies in the request in an array of named element. If there are no
     * cookies in the request, an empty array will be returned.
     * @param pRequest the request from which to extract the cookies
     * @return an array with the request cookies in it.
     */
    public static Array_Of_NamedElement<NamedElement> getCookieParameters(javax.servlet.http.HttpServletRequest pRequest) {
        Cookie[] cookies = pRequest.getCookies();
        Array_Of_NamedElement<NamedElement> result = new Array_Of_NamedElement<NamedElement>();
        for (int i = 0; i < cookies.length; i++) {
            result.add(new NamedElement(cookies[i].getName(), new TextData(cookies[i].getValue())));
        }
        _log.debug("Found " + cookies.length + " Cookie parameters");
        return result;
    }
    /**
     * The SetCookieArguments attribute contains the cookie(s) to be returned with the current HTTPResponse object.
     * Each element of the array is one cookie, or one object of type HTTPCookie.
     * To send multiple cookies for one response, you would specify multiple elements of the array of HTTPCookie.
     * @param response
     * @param cookies
     */
    public static void setCookieArguments(HttpServletResponse response, Array_Of_Cookie<Cookie> cookies){
        for (Iterator<Cookie> it = cookies.iterator(); it.hasNext();){
            Object o = it.next();
            if (o instanceof Cookie){
                response.addCookie(((Cookie)o));
            }
        }
        _log.debug("Set cookie arguments of " + cookies);
    }
    @SuppressWarnings("unchecked")
  public static Array_Of_NamedElement<NamedElement> paramsAsArray(HttpServletRequest request){
      // TF:10/3/08:Refactored this to remove redundant Array_Of_Parameter
        Array_Of_NamedElement<NamedElement> result = new Array_Of_NamedElement<NamedElement>();
        Enumeration params = request.getParameterNames();
        while ( params.hasMoreElements()) {
            String Name = (String)params.nextElement();
            String[] Values = request.getParameterValues( Name );
            for (int i = 0; i < Values.length; i++){
                result.add(new NamedElement(Name, new TextData(Values[i])));
            }
        }
        return result;
    }
    /**
     *  this method adds one cookie to the response
     *
     */
    public static List<Cookie> addCookieArgument(HttpServletResponse response){
        return new CookieList(response);
    }
   
    public static boolean isTemplateRequest(HttpServletRequest request) {
        return (HTMLScanner.getParameter(request, "TemplateName", true) != null);
    }

    public static boolean isPageRequest(HttpServletRequest request) {
        return (HTMLScanner.getParameter(request, "PageName", true) != null);
    }

}
TOP

Related Classes of HTTP.HTTPutils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.