edu.byu.wso2.apim.extensions.BYUIdentifiersLookup.java Source code

Java tutorial

Introduction

Here is the source code for edu.byu.wso2.apim.extensions.BYUIdentifiersLookup.java

Source

/*
 *    Copyright 2016 Brigham Young University
 *
 *    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 edu.byu.wso2.apim.extensions;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.ManagedLifecycle;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseLog;
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.mediators.AbstractMediator;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.*;
import java.util.Iterator;
import java.util.Set;

public class BYUIdentifiersLookup extends AbstractMediator implements ManagedLifecycle {

    private static Log log = LogFactory.getLog(BYUIdentifiersLookup.class);
    private String propertyPrefix = "";
    private String netIdPropertyToUse = "";
    private DataSource ds = null;
    private String DsName = null;

    public void init(SynapseEnvironment synapseEnvironment) {
        if (log.isInfoEnabled()) {
            log.info("Initializing BYUIdentifiersLookup Mediator");
        }
        if (log.isDebugEnabled())
            log.debug("BYUIdentifiersLookup: looking up datasource" + DsName);
        try {
            this.ds = (DataSource) new InitialContext().lookup(DsName);
        } catch (NamingException e) {
            e.printStackTrace();
        }
        if (log.isDebugEnabled())
            log.debug("BYUIdentifiersLookup: acquired datasource");

    }

    public boolean mediate(MessageContext synapseContext) {
        SynapseLog synLog = getLog(synapseContext);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Start : BYUIdentifiersLookup mediator");
            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synapseContext.getEnvelope());
            }
        }

        Connection con = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        String identifier = (String) synapseContext.getProperty(netIdPropertyToUse);

        String idType = "net_id";

        if (identifier.contains("=")) {
            String[] idPieces = identifier.split("=");
            idType = idPieces[0];
            identifier = idPieces[1];
        }

        String query;
        switch (idType) {
        case "byu_id":
            query = "select net_id, byu_id, person_id from pro.person where byu_id = ?";
            break;
        case "person_id":
            query = "select net_id, byu_id, person_id from pro.person where person_id = ?";
            break;
        case "net_id":
            query = "select net_id, byu_id, person_id from pro.person where net_id = ?";
            break;
        default:
            synLog.error("Unidentified identifier type (" + idType + ")");
            return false;
        }

        if (synLog.isTraceOrDebugEnabled())
            log.debug("BYUIdentifiersLookup: lookup starting for identifier:" + identifier + " type:" + idType);

        try {
            con = ds.getConnection();
            if (synLog.isTraceOrDebugEnabled())
                synLog.traceOrDebug("connection acquired. creating statement and executing query");
            statement = con.prepareStatement(query);
            statement.setString(1, identifier);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                String net_id = resultSet.getString("net_id");
                String byu_id = resultSet.getString("byu_id");
                String person_id = resultSet.getString("person_id");
                if (synLog.isTraceOrDebugEnabled())
                    synLog.traceOrDebug("BYUId is " + byu_id + " PersonId is " + person_id);
                synapseContext.setProperty(propertyPrefix + "NetId", net_id);
                synapseContext.setProperty(propertyPrefix + "BYUId", byu_id);
                synapseContext.setProperty(propertyPrefix + "PersonId", person_id);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    /* ignored */ }
            }
        }
        if (log.isDebugEnabled())
            log.debug("BYUIdentifiersLookup: ending");
        return true;
    }

    public void destroy() {
        if (log.isInfoEnabled()) {
            log.info("Destroying BYUIdentifiersLookup Mediator");
        }
    }

    /* propertyPrefix is used to make the property unique in the context. If a separator between the prefix and the
    property value is desired it must be included in the prefix.
     */
    public String getPropertyPrefix() {
        return propertyPrefix;
    }

    public void setPropertyPrefix(String propertyPrefix) {
        this.propertyPrefix = propertyPrefix;
    }

    /* DataSource name to use for lookup */
    public String getDsName() {
        return DsName;
    }

    public void setDsName(String dsName) {
        DsName = dsName;
    }

    /*
    name of the context property that contains the netid to use for the lookup
     */
    public String getNetIdPropertyToUse() {
        return netIdPropertyToUse;
    }

    public void setNetIdPropertyToUse(String netidPropertyToUse) {
        this.netIdPropertyToUse = netidPropertyToUse;
    }
}