Package HTTPSupport

Source Code of HTTPSupport.HTTPFactory

/*
Copyright (c) 2003-2008 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.io.Serializable;

import org.apache.log4j.Logger;

import Framework.Application;
import Framework.Array_Of_TextData;
import Framework.CloneHelper;
import Framework.DataValue;
import Framework.DistributedAccessException;
import Framework.FrameworkUtils;
import Framework.HashFuncs;
import Framework.HashTable;
import Framework.LogMgr;
import Framework.TextData;
/**
* The HTTPFactory class instantiates entities and the HTTP response and request objects. Use the factory to add new classes and make them the default classes for HTTPSupport.
*
*/
@SuppressWarnings("serial")
public class HTTPFactory
        implements Serializable
{

    private Array_Of_TextData<TextData> typeName;
    private HashTable typeValue;

    public HTTPFactory() {
        super();
        TextData txt = null;
        this.typeValue = new HashTable();
        this.typeName = new Array_Of_TextData<TextData>();

        //  Field name of headers are case insensitives (HTTP spec)
        HashFuncs hashFunctions = new HashFuncs(true, Framework.Constants.SP_KT_STRING, HashFuncs.qq_Resolver.cIGNORECASE_KEYTYPE);
        this.typeValue.setup(16, hashFunctions, true);

        //  Set the default values
        txt = new TextData(Constants.HTTP_FACTORY_REQUEST, TextData.qq_Resolver.cINTEGERVALUE);
        this.typeValue.enter(HTTPBaseRequest.class, txt.toString());

        txt = new TextData(Constants.HTTP_FACTORY_RESPONSE, TextData.qq_Resolver.cINTEGERVALUE);
        this.typeValue.enter(HTTPBaseResponse.class, txt.toString());

        txt = new TextData(Constants.HTTP_FACTORY_SESSION, TextData.qq_Resolver.cINTEGERVALUE);
        this.typeValue.enter(HTTPSession.class, txt.toString());
    }
   
    public void setTypeName(Array_Of_TextData<TextData> typeName) {
        this.typeName = typeName;
    }

    public Array_Of_TextData<TextData> getTypeName() {
        return this.typeName;
    }

    public void setTypeValue(HashTable typeValue) {
        this.typeValue = typeValue;
    }

    public HashTable getTypeValue() {
        return this.typeValue;
    }
    /**
     * The AddMimeType method registers a new MIME type that is automatically used when this MIME type is referenced in a message. Use HTTP_FACTORY_DEFAULT_ENTITY to overwrite the default MIME class type used by HTTPSupport.
     * @param mimeType
     * @param type
     */
    public void addMimeType(String mimeType, Class<?> type) {
        if (mimeType == null || type == null) {
            return;
        }

        if (this.containsMimeType(mimeType)) {
            this.typeValue.remove(mimeType);
        }
        else {
            TextData txt = new TextData(mimeType);
            this.typeName.add(txt);
        }

        this.typeValue.enter(type, mimeType);
    }
    /**
     * The ContainsMimeType method returns TRUE if the MIME type specified is already registered in the HTTPFactory.
     * @param name
     * @return
     */
    public boolean containsMimeType(String name){
        if (this.typeValue.find(name) != null) {
            return true;
        }
        else {
            return false;
        }
    }
    /**
     * The GetEntityInstance method returns an instance of the MIME type registered in the factory. If the MIME type is not registered in the factory, the method returns null.
     * @param mimeType
     * @return
     */
    public GenericEntity getEntityInstance(String mimeType){
        Class<?> classMimeType = null;
        GenericEntity entityObj = null;

        if (mimeType == null) {
            classMimeType = this.getMimeType(Constants.HTTP_FACTORY_DEFAULT_ENTITY);
        }
        else {
            classMimeType = this.getMimeType(mimeType);
        }

        if (classMimeType == null) {
            //  Use the built-in default one
            Entity entity = new Entity();
            entityObj = entity;
        }
        else {
            entityObj = (GenericEntity)FrameworkUtils.newInstance(classMimeType);
        }

        if (LogMgr.getInstance().test(Framework.Constants.SP_MT_DEBUG, Constants.HTTP_SVC, Constants.HTTP_TRACE, 10)) {
            Logger.getLogger("task.part.logmgr").info("HTTPFactory.getEntityInstance -  mimeType: ");
            Logger.getLogger("task.part.logmgr").info(mimeType);
            Logger.getLogger("task.part.logmgr").info(" - entity class: ");
            Logger.getLogger("task.part.logmgr").info(((Object)entityObj).getClass().getSimpleName());
        }

        return entityObj;
    }
    /**
     * The GetMimeType method returns the class type associated with the MIME type specified. If the MIME type is not registered in the factory, the method returns null.
     * @param mimeType
     * @return
     */
    public Class<?> getMimeType(String mimeType){
        return (Class<?>)this.typeValue.find(mimeType);
    }
    /**
     * The GetMimeTypes method returns a list of all MIME types known by the factory.
     * @return
     */
    public Array_Of_TextData<TextData> getMimeTypes() {
        return CloneHelper.clone(this.typeName, true);
    }
    /**
     * The GetObjectInstance method creates an instance of the specified object identifier. The following object identifiers are recognized:
     * HTTP_FACTORY_REQUEST
     * HTTP_FACTORY_RESPONSE
     * HTTPSupport uses this method to automatically create instances of HTTP requests and responses.
     * @param id
     * @return
     */
    public Object getObjectInstance(int id){
        TextData txt = new TextData(id, TextData.qq_Resolver.cINTEGERVALUE);
        Class<?> objClassType = (Class <?>)this.typeValue.find(txt.toString());
        Object obj = null;

        if (objClassType != null) {
            obj = FrameworkUtils.newInstance(objClassType);
        }

        if (LogMgr.getInstance().test(Framework.Constants.SP_MT_DEBUG, Constants.HTTP_SVC, Constants.HTTP_TRACE, 10)) {
            Logger.getLogger("task.part.logmgr").info("HTTPFactory.getObjectInstance -  id: ");
            Logger.getLogger("task.part.logmgr").info( Integer.toString(id));
            Logger.getLogger("task.part.logmgr").info(" - class ");
            if (obj != null) {
                Logger.getLogger("task.part.logmgr").info(obj.getClass().getSimpleName());
            }
            else {
                Logger.getLogger("task.part.logmgr").info("<not found>");
            }
        }

        return obj;
    }

    /**
     * The RegisterClass method registers a class type for a specific identifier in the factory. The id is optional if the class type is known for the factory. For example, no identifier is needed to register a subclass of HTTPBaseRequest and HTTPBaseResponse because the factory recognizes these classes. If you use the identifier for these classes, the pre-defined constant values HTTP_FACTORY_REQUEST and HTTP_FACTORY_RESPONSE are required.
     * @param type
     */
    public void registerClass(Class<?> type){
        this.registerClass(type, 0);
    }

    /**
     * The RegisterClass method registers a class type for a specific identifier in the factory. The id is optional if the class type is known for the factory. For example, no identifier is needed to register a subclass of HTTPBaseRequest and HTTPBaseResponse because the factory recognizes these classes. If you use the identifier for these classes, the pre-defined constant values HTTP_FACTORY_REQUEST and HTTP_FACTORY_RESPONSE are required.
     * @param type
     * @param id
     */
    public void registerClass(Class<?> type, int id){
        Object typeInstance = FrameworkUtils.newInstance(type);
        TextData txt = null;
        TextData txt2 = null;

        //  Type and id checking for reserved values
        if (id > 0) {
            boolean error = false;

            switch (id) {
                case Constants.HTTP_FACTORY_RESPONSE: {
                    if (typeInstance instanceof HTTPBaseResponse == false) {
                        error = true;
                        txt2 = new TextData("HTTPBaseResponse");
                    }

                    break;
                }
                case Constants.HTTP_FACTORY_REQUEST: {
                    if (typeInstance instanceof HTTPBaseRequest == false) {
                        error = true;
                        txt2 = new TextData("HTTPBaseRequest");
                    }
                    break;
                }
                case Constants.HTTP_FACTORY_SESSION: {
                    if (typeInstance instanceof HTTPSession == false) {
                        error = true;
                        txt2 = new TextData("HTTPSession");
                    }
                    break;
                }
            }

            if (error == true) {
                DistributedAccessException ex = new DistributedAccessException();
                txt = new TextData(FrameworkUtils.getClassName(type,false));

                ex.setWithParams(Framework.Constants.SP_ER_USER, new TextData(Application.getMsgCatalog().getString(Constants.HTTP_SET, Constants.HTTP_MSG_FACTORY_NOT_HTTP_MESSAGE)), txt, txt2, (DataValue)null, (DataValue)null, (DataValue)null, (DataValue)null, (DataValue)null, (DataValue)null, (DataValue)null);
                throw ex;
            }
        }

        if (typeInstance instanceof HTTPBaseRequest) {
            txt = new TextData(Constants.HTTP_FACTORY_REQUEST, TextData.qq_Resolver.cINTEGERVALUE);
            txt2 = new TextData("HTTPBaseRequest");

        }
        else if (typeInstance instanceof HTTPBaseResponse) {
            txt = new TextData(Constants.HTTP_FACTORY_RESPONSE, TextData.qq_Resolver.cINTEGERVALUE);
            txt2 = new TextData("HTTPBaseResponse ");

        }
        else if (typeInstance instanceof HTTPSession) {
            txt = new TextData(Constants.HTTP_FACTORY_SESSION, TextData.qq_Resolver.cINTEGERVALUE);
            txt2 = new TextData("HTTPSession ");

        }
        else if (id > 0) {
            txt = new TextData(id, TextData.qq_Resolver.cINTEGERVALUE);
            txt2 = txt;
        }
        else {
            return;
        }

        this.typeValue.remove(txt.toString());
        this.typeValue.enter(type, txt.toString());

        if (LogMgr.getInstance().test(Framework.Constants.SP_MT_DEBUG, Constants.HTTP_SVC, Constants.HTTP_TRACE, 10)) {
            Logger.getLogger("task.part.logmgr").info("HTTPFactory.registerClass -  registered ");
            Logger.getLogger("task.part.logmgr").info(FrameworkUtils.getClassName(type,false));
            Logger.getLogger("task.part.logmgr").info(" as the new type ");
            Logger.getLogger("task.part.logmgr").info(txt2);
        }
    }
}
TOP

Related Classes of HTTPSupport.HTTPFactory

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.