egovframework.rte.itl.webservice.EgovWebService.java Source code

Java tutorial

Introduction

Here is the source code for egovframework.rte.itl.webservice.EgovWebService.java

Source

/*
 * Copyright 2008-2009 MOPAS(Ministry of Public Administration and Security).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package egovframework.rte.itl.webservice;

import java.util.Calendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import egovframework.rte.itl.integration.EgovIntegrationMessage;
import egovframework.rte.itl.integration.EgovIntegrationMessageHeader.ResultCode;
import egovframework.rte.itl.integration.metadata.IntegrationDefinition;
import egovframework.rte.itl.integration.metadata.OrganizationDefinition;
import egovframework.rte.itl.integration.metadata.ServiceDefinition;
import egovframework.rte.itl.integration.metadata.SystemDefinition;
import egovframework.rte.itl.integration.support.AbstractService;
import egovframework.rte.itl.webservice.service.EgovWebServiceClient;

/**
 * ?  ?    ?
 * <p>
 * <b>NOTE:</b> ?  ?   class?.
 * @author   ?
 * @since 2009.06.01
 * @version 1.0
 * @see <pre>
 *  == ?(Modification Information) ==
 *   
 *   ?      ?           
 *  -------    --------    ---------------------------
 *   2009.06.01  ?            ?
 * 
 * </pre>
 */
public class EgovWebService extends AbstractService {
    private Log LOG = LogFactory.getLog(this.getClass());

    /**  ? */
    private final IntegrationDefinition integrationDefinition;

    /**   ? */
    private final EgovWebServiceMessageHeader defaultHeader;

    /** EgovWebServiceClient */
    private EgovWebServiceClient client;

    /**
     * Constructor
     * @param id
     *         ID
     * @param defaultTimeout
     *        default timeout
     * @param integrationDefinition
     *         ?
     * @param client
     *        client
     * @throws IllegalArgumentException
     *         1. Argument <code>id</code>,
     *         <code>integrationDefinition</code>,
     *         <code>client</code> ? <code>null</code>
     *         ?  <br>
     *         2. MessageHeader    ? ? 
     *           <br>
     *         3. Argument <code>id</code>
     *         <code>integrationDefinition.id</code> ?
     *          
     */
    public EgovWebService(String id, long defaultTimeout, IntegrationDefinition integrationDefinition,
            EgovWebServiceClient client) {
        super(id, defaultTimeout);
        if (integrationDefinition == null) {
            LOG.error("integrationDefinition is null");
            throw new IllegalArgumentException();
        } else if (integrationDefinition.isValid() == false) {
            LOG.error("integrationDefinition is invalid");
            throw new IllegalArgumentException();
        } else if (id.equals(integrationDefinition.getId()) == false) {
            LOG.error("id not equals to integrationDefinition's id");
            throw new IllegalArgumentException();
        } else if (client == null) {
            LOG.error("client is null");
            throw new IllegalArgumentException();
        }
        this.integrationDefinition = integrationDefinition;
        this.client = client;

        // need not check, is validated by
        // integrationDefinition.isValid
        ServiceDefinition providerService = integrationDefinition.getProvider();
        // if (providerService == null)
        // {
        // LOG.error("integrationDefinition.provider is null");
        // throw new IllegalArgumentException();
        // }
        SystemDefinition providerSystem = providerService.getSystem();
        // if (providerSystem == null)
        // {
        // LOG.error("integrationDefinition.provider.system is null");
        // throw new IllegalArgumentException();
        // }
        OrganizationDefinition providerOrganization = providerSystem.getOrganization();
        // if (providerOrganization == null)
        // {
        // LOG.error("integrationDefinition.provider.system.organization "
        // +
        // "is null");
        // throw new IllegalArgumentException();
        // }
        SystemDefinition consumerSystem = integrationDefinition.getConsumer();
        // if (consumerSystem == null)
        // {
        // LOG.error("integrationDefinition.consumer is null");
        // throw new IllegalArgumentException();
        // }
        OrganizationDefinition consumerOrganization = consumerSystem.getOrganization();
        // if (consumerOrganization == null)
        // {
        // LOG.error("integrationDefinition.consumer.organization is null");
        // throw new IllegalArgumentException();
        // }
        this.defaultHeader = new EgovWebServiceMessageHeader(id, providerOrganization.getId(),
                providerSystem.getId(), providerService.getId(), consumerOrganization.getId(),
                consumerSystem.getId(), null, null, null, null, ResultCode.OK, null);
    }

    public EgovIntegrationMessage createRequestMessage() {
        return new EgovWebServiceMessage(new EgovWebServiceMessageHeader(defaultHeader));
    }

    @Override
    protected EgovIntegrationMessage doSend(EgovIntegrationMessage requestMessage) {
        LOG.debug("EgovWebSerivce doSend (requestMessage = " + requestMessage + ")");

        //   ?.
        if (integrationDefinition.isUsing() == false) {
            LOG.info("Integration (id = \"" + id + "\") is not usable");
            return new EgovWebServiceMessage(new EgovWebServiceMessageHeader(defaultHeader) {
                {
                    setResultCode(ResultCode.NOT_USABLE_INTEGRATION);
                }
            });
        }

        //   ? ?.
        boolean validFrom = true;
        boolean validTo = true;
        Calendar now = Calendar.getInstance();
        if (integrationDefinition.getValidateFrom() != null) {
            validFrom = (integrationDefinition.getValidateFrom().compareTo(now) <= 0);
        }
        if (integrationDefinition.getValidateTo() != null) {
            validTo = (now.compareTo(integrationDefinition.getValidateTo()) <= 0);
        }
        if (validFrom == false || validTo == false) {
            LOG.info("Integration (id = \"" + id + "\") is invalid at " + now + " (validFrom = "
                    + integrationDefinition.getValidateFrom() + ", validTo = "
                    + integrationDefinition.getValidateTo() + ")");
            return new EgovWebServiceMessage(new EgovWebServiceMessageHeader(defaultHeader) {
                {
                    setResultCode(ResultCode.INVALID_TIME);
                }
            });
        }

        // ?    ?.
        if (integrationDefinition.getProvider().isUsing() == false) {
            LOG.info("Integration (id = \"" + id + "\")'s provider service " + "is not usable");
            return new EgovWebServiceMessage(new EgovWebServiceMessageHeader(defaultHeader) {
                {
                    setResultCode(ResultCode.NOT_USABLE_SERVICE);
                }
            });
        }

        //   ? 
        requestMessage.getHeader().setRequestSendTime(now);

        //   & ? 
        EgovIntegrationMessage responseMessage = client.service(requestMessage);

        // ?  ? 
        responseMessage.getHeader().setResponseReceiveTime(Calendar.getInstance());

        return responseMessage;
    }
}