org.mule.modules.vertex.impl.SimpleVertexClient.java Source code

Java tutorial

Introduction

Here is the source code for org.mule.modules.vertex.impl.SimpleVertexClient.java

Source

/**
 * Mule Vertex Connector
 *
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

package org.mule.modules.vertex.impl;

import org.mule.modules.vertex.TaxGisType;
import org.mule.modules.vertex.TaxTransactionSyncType;
import org.mule.modules.vertex.TaxTransactionType;
import org.mule.modules.vertex.api.VertexClient;
import org.mule.modules.vertex.exception.VertexRuntimeException;
import org.mule.modules.vertex.util.VertexEnvelopeBuilder;

import com.vertexinc.oseries.services.calculatetax60.CalculateTaxWS60;
import com.vertexinc.oseries.services.calculatetax60.CalculateTaxWSService60;
import com.vertexinc.oseries.services.echodoc.EchoWS;
import com.vertexinc.oseries.services.echodoc.EchoWSService;
import com.vertexinc.oseries.services.lookuptaxareas60.LookupTaxAreasWS60;
import com.vertexinc.oseries.services.lookuptaxareas60.LookupTaxAreasWSService60;
import com.zauberlabs.commons.ws.connection.ConnectionBuilder;
import com.zauberlabs.commons.ws.connection.cxf.TrafficDebugging;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.Validate;

import vertexinc.o_series.tps._6._0.LoginType;
import vertexinc.o_series.tps._6._0.TaxSynchronizationResponseType;
import vertexinc.o_series.tps._6._0.TaxTransactionResponseType;
import vertexinc.o_series.tps._6._0.TaxgisResponseType;
import vertexinc.o_series.tps._6._0.VertexEnvelope;

/**
 * @author Pablo Diez 
 * @since 14/03/2012
 */
public class SimpleVertexClient implements VertexClient {

    private CalculateTaxWS60 calculateTaxSvc;
    private LookupTaxAreasWS60 lookupTaxSvc;
    private EchoWS echoSvc;

    private final String username;
    private final String password;
    private final String trustedId;

    public SimpleVertexClient(String username, String password, String trustedId) {
        Validate.notNull(username);
        Validate.notNull(password);

        this.username = username;
        this.password = password;
        this.trustedId = trustedId;
    }

    /**
     * @see org.mule.modules.vertex.api.VertexClient#calculateTax(TaxTransactionType, Object)
     */
    @Override
    @SuppressWarnings("unchecked")
    public <T extends TaxTransactionResponseType> T calculateTax(TaxTransactionType type, Object request) {
        try {
            VertexEnvelope vertexEnvelope = new VertexEnvelopeBuilder().withRequest(type, request)
                    .withLogin(createLogin()).build();
            VertexEnvelope response = getCalculateTaxService().calculateTax60(vertexEnvelope);
            return (T) PropertyUtils.getSimpleProperty(response, type.getResponseAttr());
        } catch (Exception e) {
            throw new VertexRuntimeException(e);
        }
    }

    /**
     * @see org.mule.modules.vertex.api.VertexClient#calculateTaxSync(TaxTransactionSyncType, Object)
     */
    @Override
    @SuppressWarnings("unchecked")
    public <T extends TaxSynchronizationResponseType> T calculateTaxSync(TaxTransactionSyncType type,
            Object request) {
        try {
            VertexEnvelope vertexEnvelope = new VertexEnvelopeBuilder().withRequest(type, request)
                    .withLogin(createLogin()).build();
            VertexEnvelope response = getCalculateTaxService().calculateTax60(vertexEnvelope);
            return (T) PropertyUtils.getSimpleProperty(response, type.getResponseAttr());
        } catch (Exception e) {
            throw new VertexRuntimeException(e);
        }
    }

    /**
     * @see org.mule.modules.vertex.api.VertexClient#lookupTaxArea(TaxGisType,Object)
     */
    @Override
    @SuppressWarnings("unchecked")
    public <T extends TaxgisResponseType> T lookupTaxArea(TaxGisType type, Object request) {
        try {
            VertexEnvelope vertexEnvelope = new VertexEnvelopeBuilder().withRequest(type, request)
                    .withLogin(createLogin()).build();
            VertexEnvelope response = getLookupTaxAreasService().lookupTaxAreas60(vertexEnvelope);
            return (T) PropertyUtils.getSimpleProperty(response, type.getResponseAttr());
        } catch (Exception e) {
            throw new VertexRuntimeException(e);
        }
    }

    /** @see org.mule.modules.vertex.api.VertexClient#pingService(java.lang.String) */
    @Override
    public Object pingService(String echo) {
        Validate.notNull(echo);
        Validate.notEmpty(echo);
        return getEchoService().echo(echo);
    }

    protected CalculateTaxWS60 getCalculateTaxService() {
        if (calculateTaxSvc == null) {
            calculateTaxSvc = createConnection(CalculateTaxWS60.class, CalculateTaxWSService60.class,
                    "CalculateTax60", CalculateTaxWSService60.CalculateTax60);
        }
        return calculateTaxSvc;
    }

    protected LookupTaxAreasWS60 getLookupTaxAreasService() {
        if (lookupTaxSvc == null) {
            lookupTaxSvc = createConnection(LookupTaxAreasWS60.class, LookupTaxAreasWSService60.class,
                    "LookupTaxAreas60", LookupTaxAreasWSService60.LookupTaxAreas60);
        }
        return lookupTaxSvc;
    }

    protected EchoWS getEchoService() {
        if (echoSvc == null) {
            echoSvc = createConnection(EchoWS.class, EchoWSService.class, "EchoDoc", EchoWSService.Echo);
        }
        return echoSvc;
    }

    protected <A> A createConnection(Class<A> portType, Class<? extends Service> serviceType, String schemaName,
            QName portName) {
        return ConnectionBuilder.fromPortType(portType).withServiceType(serviceType)
                .withClasspathWsdl(schemaLocation(schemaName)).withPortQName(portName)
                .withConfiguration(TrafficDebugging.toSysout()).build();
    }

    protected String schemaLocation(String schemaName) {
        return "schema/" + schemaName + ".wsdl";
    }

    protected LoginType createLogin() {
        LoginType login = new LoginType();
        login.setUserName(this.username);
        login.setPassword(this.password);
        login.setTrustedId(this.trustedId);
        return login;
    }

}