Search in sources :

Example 26 with SynapseException

use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.

the class RabbitMQStore method initme.

private boolean initme() {
    Set<Map.Entry<String, Object>> mapSet = parameters.entrySet();
    for (Map.Entry<String, Object> e : mapSet) {
        if (e.getValue() instanceof String) {
            properties.put(e.getKey(), e.getValue());
        }
    }
    userName = (String) parameters.get(USERNAME);
    password = (String) parameters.get(PASSWORD);
    hostName = (String) parameters.get(HOST_NAME);
    hostPort = (String) parameters.get(HOST_PORT);
    virtualHost = (String) parameters.get(VIRTUAL_HOST);
    // Possible timeouts that can be added in future if requested, should be added to the
    // setConnectionTimeout, ShutdownTimeout, RequestedHeartbeat
    connectionFactory = new ConnectionFactory();
    if (hostName != null && !hostName.equals("")) {
        connectionFactory.setHost(hostName);
    } else {
        throw new SynapseException(nameString() + " host name is not correctly defined");
    }
    int port = 0;
    try {
        port = Integer.parseInt(hostPort);
    } catch (NumberFormatException nfe) {
        logger.error("Port value for " + nameString() + " is not correctly defined" + nfe);
    }
    if (port > 0) {
        connectionFactory.setPort(port);
    } else {
        connectionFactory.setPort(DEFAULT_PORT);
        logger.info(nameString() + " port is set to default value (5672");
    }
    if (userName != null && !userName.equals("")) {
        connectionFactory.setUsername(userName);
    }
    if (password != null && !password.equals("")) {
        connectionFactory.setPassword(password);
    }
    if (virtualHost != null && !virtualHost.equals("")) {
        connectionFactory.setVirtualHost(virtualHost);
    }
    String sslEnabledS = parameters.get(SSL_ENABLED) != null ? parameters.get(SSL_ENABLED).toString() : "";
    if (!StringUtils.isEmpty(sslEnabledS)) {
        try {
            boolean sslEnabled = Boolean.parseBoolean(sslEnabledS);
            if (sslEnabled) {
                String keyStoreLocation = parameters.get(SSL_KEYSTORE_LOCATION) != null ? parameters.get(SSL_KEYSTORE_LOCATION).toString() : "";
                String keyStoreType = parameters.get(SSL_KEYSTORE_TYPE) != null ? parameters.get(SSL_KEYSTORE_TYPE).toString() : "";
                String keyStorePassword = parameters.get(SSL_KEYSTORE_PASSWORD) != null ? parameters.get(SSL_KEYSTORE_PASSWORD).toString() : "";
                String trustStoreLocation = parameters.get(SSL_TRUSTSTORE_LOCATION) != null ? parameters.get(SSL_TRUSTSTORE_LOCATION).toString() : "";
                String trustStoreType = parameters.get(SSL_TRUSTSTORE_TYPE) != null ? parameters.get(SSL_TRUSTSTORE_TYPE).toString() : "";
                String trustStorePassword = parameters.get(SSL_TRUSTSTORE_PASSWORD) != null ? parameters.get(SSL_TRUSTSTORE_PASSWORD).toString() : "";
                String sslVersion = parameters.get(SSL_VERSION) != null ? parameters.get(SSL_VERSION).toString() : "";
                if (StringUtils.isEmpty(keyStoreLocation) || StringUtils.isEmpty(keyStoreType) || StringUtils.isEmpty(keyStorePassword) || StringUtils.isEmpty(trustStoreLocation) || StringUtils.isEmpty(trustStoreType) || StringUtils.isEmpty(trustStorePassword)) {
                    logger.warn("Trustore and keystore information is not provided correctly. Proceeding with default SSL configuration");
                    connectionFactory.useSslProtocol();
                } else {
                    char[] keyPassphrase = keyStorePassword.toCharArray();
                    KeyStore ks = KeyStore.getInstance(keyStoreType);
                    ks.load(new FileInputStream(keyStoreLocation), keyPassphrase);
                    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                    kmf.init(ks, keyPassphrase);
                    char[] trustPassphrase = trustStorePassword.toCharArray();
                    KeyStore tks = KeyStore.getInstance(trustStoreType);
                    tks.load(new FileInputStream(trustStoreLocation), trustPassphrase);
                    TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                    tmf.init(tks);
                    SSLContext c = SSLContext.getInstance(sslVersion);
                    c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
                    connectionFactory.useSslProtocol(c);
                }
            }
        } catch (Exception e) {
            logger.warn("Format error in SSL enabled value. Proceeding without enabling SSL", e);
        }
    }
    // declaring queue
    String queueName = (String) parameters.get(QUEUE_NAME);
    if (queueName != null) {
        this.queueName = queueName;
    } else {
        String name = getName();
        String defaultQueue;
        if (name != null && !name.isEmpty()) {
            defaultQueue = name + "_Queue";
        } else {
            defaultQueue = "RabiitmqStore_" + System.currentTimeMillis() + "_Queue";
        }
        logger.warn(nameString() + ". Destination not provided. " + "Setting default destination to [" + defaultQueue + "].");
        this.queueName = defaultQueue;
    }
    exchangeName = (String) properties.get(EXCHANGE_NAME);
    routeKey = (String) properties.get(ROUTE_KEY);
    if (routeKey == null) {
        logger.warn(nameString() + ". Routing key is not provided. " + "Setting queue name " + this.queueName + " as routing key.");
        routeKey = this.queueName;
    }
    if (!newProducerConnection()) {
        logger.warn(nameString() + ". Starting with a faulty connection to the broker.");
        return false;
    }
    try {
        setQueue();
    } catch (IOException e) {
        logger.error(nameString() + " error in storage declaring queue " + queueName);
        return false;
    }
    return true;
}
Also used : SynapseException(org.apache.synapse.SynapseException) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) TimeoutException(java.util.concurrent.TimeoutException) NoSuchElementException(java.util.NoSuchElementException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) Map(java.util.Map)

Example 27 with SynapseException

use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.

the class ResequenceMessageStore method getNextMessage.

/**
 * Will get the next message belonging to a sequence.
 *
 * @return the next message in the sequence.
 */
private MessageContext getNextMessage() {
    MessageContext msg = null;
    final int firstRowIndex = 0;
    try {
        String tableName = getJdbcConfiguration().getTableName();
        String selectMessageStatement = "SELECT message FROM " + tableName + " WHERE " + ResequenceMessageStoreConstants.SEQ_ID + "= ?";
        Statement statement = new Statement(selectMessageStatement) {

            @Override
            public List<Map> getResult(ResultSet resultSet) throws SQLException {
                return messageContentResultSet(resultSet, this.getStatement());
            }
        };
        statement.addParameter(nextSequenceId);
        List<Map> processedRows = getProcessedRows(statement);
        if (!processedRows.isEmpty()) {
            msg = getMessageContext(processedRows, firstRowIndex);
            nextSequenceId++;
            if (log.isTraceEnabled()) {
                log.trace("Message with id " + msg.getMessageID() + " returned for sequence " + nextSequenceId);
            }
        } else {
            if (log.isTraceEnabled()) {
                log.trace("Sequences not returned from DB, next sequence will be:" + nextSequenceId);
            }
        }
    } catch (SynapseException ex) {
        throw new SynapseException("Error while peek the message", ex);
    }
    return msg;
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) ResultSet(java.sql.ResultSet) MessageContext(org.apache.synapse.MessageContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with SynapseException

use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.

the class ResequenceMessageStore method getMessageWithMinimumSequence.

/**
 * <p>
 * Retrieve the minimum sequence number of the available sequence ids in the DB
 * </p>
 * <p>
 * This operation should call when there's a gap and a timeout occurs.
 * </p>
 * <p>
 * <b>Note : </b> This operation would reset the "nextSequenceId" to the minimum sequence id generated from the
 * DB.
 * </p>
 *
 * @return the message context of the next sequence
 */
private MessageContext getMessageWithMinimumSequence() {
    String tableName = getJdbcConfiguration().getTableName();
    String selectMinimumSequenceIdStatement = "SELECT message,seq_id FROM " + tableName + " WHERE " + ResequenceMessageStoreConstants.SEQ_ID + "=(SELECT min(" + ResequenceMessageStoreConstants.SEQ_ID + ")" + " from " + tableName + ")";
    Statement stmt = new Statement(selectMinimumSequenceIdStatement) {

        @Override
        public List<Map> getResult(ResultSet resultSet) throws SQLException {
            return getMessageWithMinimumId(resultSet, this.getStatement());
        }
    };
    MessageContext msg = null;
    final int firstRowIndex = 0;
    try {
        List<Map> processedRows = getProcessedRows(stmt);
        if (!processedRows.isEmpty()) {
            msg = getMessageContext(processedRows, firstRowIndex);
            long sequenceId = getSequenceId(processedRows, firstRowIndex);
            nextSequenceId = sequenceId + 1;
            if (log.isTraceEnabled()) {
                log.trace("Message with id " + msg.getMessageID() + " returned as the minimum, the minimum " + "sequence " + "will be marked as " + nextSequenceId);
            }
        }
    } catch (SynapseException ex) {
        throw new SynapseException("Error while peek the message", ex);
    }
    return msg;
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) ResultSet(java.sql.ResultSet) MessageContext(org.apache.synapse.MessageContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with SynapseException

use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.

the class RegistryEntryImpl method setType.

public void setType(String type) {
    try {
        new ContentType(type);
        if (log.isDebugEnabled()) {
            log.debug("Content type :" + type);
        }
        this.type = type;
    } catch (ParseException e) {
        String msg = "Invalid content-type ' " + type + " '";
        throw new SynapseException(msg, e);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) SynapseException(org.apache.synapse.SynapseException) ParseException(javax.mail.internet.ParseException)

Example 30 with SynapseException

use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.

the class SimpleURLRegistry method getChildren.

public RegistryEntry[] getChildren(RegistryEntry entry) {
    URL url;
    if (entry == null) {
        RegistryEntryImpl entryImpl = new RegistryEntryImpl();
        entryImpl.setKey("");
        entry = entryImpl;
    }
    url = SynapseConfigUtils.getURLFromPath(root + entry.getKey(), properties.get(SynapseConstants.SYNAPSE_HOME) != null ? properties.get(SynapseConstants.SYNAPSE_HOME).toString() : "");
    if (url == null) {
        return null;
    }
    if (url.getProtocol().equals("file")) {
        File file = new File(url.getFile());
        if (!file.isDirectory()) {
            return null;
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader((InputStream) url.getContent()));
            ArrayList<RegistryEntry> entryList = new ArrayList<RegistryEntry>();
            String key;
            while ((key = reader.readLine()) != null) {
                RegistryEntryImpl registryEntryImpl = new RegistryEntryImpl();
                if (entry.getKey().equals("")) {
                    registryEntryImpl.setKey(key);
                } else {
                    if (entry.getKey().endsWith("/")) {
                        registryEntryImpl.setKey(entry.getKey() + key);
                    } else {
                        registryEntryImpl.setKey(entry.getKey() + "/" + key);
                    }
                }
                entryList.add(registryEntryImpl);
            }
            RegistryEntry[] entries = new RegistryEntry[entryList.size()];
            for (int i = 0; i < entryList.size(); i++) {
                entries[i] = entryList.get(i);
            }
            return entries;
        } catch (Exception e) {
            throw new SynapseException("Error in reading the URL.");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ignored) {
                }
            }
        }
    } else {
        throw new SynapseException("Invalid protocol.");
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) ArrayList(java.util.ArrayList) RegistryEntryImpl(org.apache.synapse.registry.RegistryEntryImpl) RegistryEntry(org.apache.synapse.registry.RegistryEntry) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) SynapseException(org.apache.synapse.SynapseException) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

SynapseException (org.apache.synapse.SynapseException)136 OMElement (org.apache.axiom.om.OMElement)31 OMAttribute (org.apache.axiom.om.OMAttribute)23 MessageContext (org.apache.synapse.MessageContext)20 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)16 QName (javax.xml.namespace.QName)15 Iterator (java.util.Iterator)14 JaxenException (org.jaxen.JaxenException)14 XMLStreamException (javax.xml.stream.XMLStreamException)13 AxisFault (org.apache.axis2.AxisFault)13 Map (java.util.Map)12 Endpoint (org.apache.synapse.endpoints.Endpoint)12 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)8 OMNode (org.apache.axiom.om.OMNode)7 Mediator (org.apache.synapse.Mediator)7 MediatorProperty (org.apache.synapse.mediators.MediatorProperty)7