Package HTTPSupport

Source Code of HTTPSupport.HeaderHolder

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

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

import Framework.Array_Of_TextData;
import Framework.TextData;

/**
* The HeaderHolder class provides message header management services. It
* implements the {@link GenericHeaderHolder} interface. The HeaderHolder class is the
* base class for the {@link HTTPBaseMessage} and {@link Entity} classes.
*
*/
public class HeaderHolder implements GenericHeaderHolder {

    private Set<String> headerName = new HashSet<String>();

    private Hashtable<String, Object> headerValue = new Hashtable<String, Object>();

    public HeaderHolder() {
        super();
        //        HashFuncs strFunc = new HashFuncs(Constants.SP_KT_STRING);
        //        this.headerValue = new HashTable();
        //        this.headerValue.setup(20, strFunc, false); // the keys are not
        // unique allowing multiple values
        //        this.headerName = new HashSet(); // Set is an interface so can not be
        // instantiated directly
    }

    /**
     * The AddHeaderValue method adds a new header name and value without
     * replacing a possible existing header name. Use this method for
     * multiple-value headers. If the value is NIL, nothing is added or removed
     *
     * @param name
     *            The name parameter specifies the name of the header
     * @param value
     *            The value parameter specifies the value of the header
     */
    public void addHeaderValue(String name, String value) {
        throw new UnsupportedOperationException("HeaderHolder.addHeaderValue() is not implemented");
    }

    /**
     * The ContainsHeader method returns TRUE if it finds a header.
     *
     * @param name
     *            The name parameter specifies the name of the header
     * @return TRUE if it finds a header
     */
    public boolean containsHeader(String name) {
        Object obj = this.getHeader(name);
        if (obj != null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * The GetHeader method gets the value of the header name. The method
     * returns NIL if there is no header of this name, or if the parameter name
     * is NIL. If the header contains an empty value, the result is an empty
     * string. The GetHeader method returns only the first value of the header.
     * If the header is a multiple-value header (multiple headers with the same
     * name), call the method GetHeaderValues to get all the values.
     *
     * @param name
     *            The name parameter specifies the name of the header
     * @return The value of the header name
     */
    public String getHeader(String name) {
        if (name == null) {
            return null;
        }
        Object obj = this.headerValue.get(name);
        if (obj != null) {
            return (String) (obj);
        } else {
            return null;
        }
    }

    /**
     * The GetHeaderAttribute method returns the value of any attribute parts of
     * a header. The attribute must use the syntax attributeName =
     * attributeValue. This method looks for the attribute name, skipping any
     * blank character, and then for the equal (=) sign. It next looks for a
     * delimiter value, considering any characters between the equal sign
     * (except blank characters) and the delimiter sign (indicated by the
     * delimiter parameter). If it does not find an attribute, it returns NIL.
     * If it finds an attribute, but not the equal sign, it returns an empty
     * string.
     *
     * @param name
     *            The name parameter specifies the name of the header
     * @param attributeName
     *            The attributeName parameter specifies the name of the
     *            attribute to extract from the header name value
     * @param ignoreCase
     *            The ignoreCase parameter specifies how to search for the
     *            attribute name. The default is ignoreCase = TRUE
     * @param delimeter
     *            The delimiter parameter defines the characters separating the
     *            attributes if there are several attributes within the header
     *            value. The default delimiters are space and semi-colon
     * @return The value of any attribute parts of a header
     */
    public String getHeaderAttribute(String name, String attributeName,
            boolean ignoreCase, String delimiter) {
        throw new UnsupportedOperationException("HeaderHolder.getHeaderAttribute() is not implemented");
    }

    /**
     * The GetHeaderAttributeValue method is similar to the GetHeaderAttribute
     * method. Instead of using the name of the header as the first parameter,
     * this method uses the value of the header. This method is useful for
     * parsing multiple-value headers.
     *
     * @param value
     *            The value parameter specifies the name of the header
     * @param attributeName
     *            The attributeName parameter specifies the name of the
     *            attribute to extract from the header name value
     * @param ignoreCase
     *            The ignoreCase parameter defines how the attribute name must
     *            be searched. The default is ignoreCase = TRUE
     * @param delimeter
     *            The delimiter parameter defines the characters separating the
     *            attributes if there are several attributes within the header
     *            value. The default delimiters are space and semi-colon
     * @return The value of any attribute parts of a header using the value of
     *         the header as the first parameter
     */
    public String getHeaderAttributeValue(String value, String attributeName,
            boolean ignoreCase, String delimiter) {
        throw new UnsupportedOperationException("HeaderHolder.getHeaderAttributeValue() is not implemented");
    }

    /**
     * The GetHeaderNames method lists all message header names.
     *
     * @return List of all message header names
     */
    public Array_Of_TextData<TextData> getHeaderNames() {
        Array_Of_TextData<TextData> headerNames = new Array_Of_TextData<TextData>();
        Iterator<String> itr = this.headerName.iterator();
        while (itr.hasNext()) {
            headerNames.add(new TextData((String) itr.next()));
        }
        return headerNames;
    }

    /**
     * The GetHeaderValues method returns all the values of the header name. Use
     * this method instead of GetHeader to return the values of a multiple-value
     * header. If there is no header name, the method returns an empty array.
     *
     * @param name
     *            The name parameter specifies the name of the header
     * @return List of all values
     */
    public Array_Of_TextData<TextData> getHeaderValues(String name) {
        //    Array_Of_TextData textList = new Array_Of_TextData();
        //   
        //    if (name != null) {
        //      Array_Of_Object objList = this.headerValue.findAll(name);
        //      if (objList != null) {
        //        String tmpString;
        //        for (int i=0, sz=objList.size(); i<sz; i++) {
        //          tmpString = (String) objList.get(i);
        //          textList.add(new TextData(tmpString));
        //        }
        //      }
        //    }
        //    return textList;
        throw new UnsupportedOperationException("HeaderHolder.getHeaderValues() is not implemented");
    }

    /**
     * The GetIntHeader method returns the integer value of the header name.
     * This method is useful for headers with an integer value, such as the
     * Content-Length header. If the header is not found, the method returns the
     * value 0.
     *
     * @param name
     *            The name parameter specifies the name of the header
     * @return The integer value of the header name
     */
    public int getIntHeader(String name) {
        String tmpString = this.getHeader(name);
        if (tmpString == null) {
            return 0;
        } else {
            return Integer.parseInt(tmpString);
        }
    }

    /**
     * The GetTextHeader method is similar to the GetHeader method, but it
     * returns TextData instead of a string.
     *
     * @param name
     *            The name parameter specifies the name of the header
     * @return The value of the header name returned as a TextData
     */
    public TextData getTextHeader(String name) {
        if (this.getHeader(name) != null) {
            return new TextData(this.getHeader(name));
        } else {
            return null;
        }
    }

    /**
     * The SetHeader method sets the header name by using the second parameter
     * value. It replaces any existing header with the same name. If the value
     * is NIL, it removes the header. To set multiple headers using the same
     * header name, use AddHeaderValue instead of SetHeader.
     *
     * @param name
     *            The name parameter specifies the name of the header
     * @param value
     *            The value parameter specifies the value of the header
     */
    public void setHeader(String name, String value) {
        if (value != null) {
            this.headerValue.put(name, value);
            this.headerName.add(name);
        }
    }

    /**
     * The SetIntHeader method is similar to the SetHeader method. This method
     * provides a convenient way to set integer value headers.
     *
     * @param name
     *            The name parameter specifies the name of the header
     * @param value
     *            The value parameter specifies the integer value of the header
     */
    public void setIntHeader(String name, int value) {
        this.setHeader(name, String.valueOf(value));
    }

}
TOP

Related Classes of HTTPSupport.HeaderHolder

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.