Search in sources :

Example 41 with OMException

use of org.apache.axiom.om.OMException in project carbon-apimgt by wso2.

the class APIProviderImpl method getCustomApiOutSequences.

/**
 * Get the list of Custom out Sequences of API
 *
 * @return List of available out sequences
 * @throws APIManagementException
 */
public List<String> getCustomApiOutSequences(APIIdentifier apiIdentifier) throws APIManagementException {
    Set<String> sequenceList = new TreeSet<>();
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = null;
        if (apiIdentifier.getProviderName().contains("-AT-")) {
            String provider = apiIdentifier.getProviderName().replace("-AT-", "@");
            tenantDomain = MultitenantUtils.getTenantDomain(provider);
        }
        PrivilegedCarbonContext.startTenantFlow();
        isTenantFlowStarted = true;
        if (!StringUtils.isEmpty(tenantDomain)) {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        } else {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
        }
        UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
        String customOutSeqFileLocation = APIUtil.getSequencePath(apiIdentifier, APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT);
        if (registry.resourceExists(customOutSeqFileLocation)) {
            org.wso2.carbon.registry.api.Collection outSeqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(customOutSeqFileLocation);
            if (outSeqCollection != null) {
                String[] outSeqChildPaths = outSeqCollection.getChildren();
                Arrays.sort(outSeqChildPaths);
                for (String outSeqChildPath : outSeqChildPaths) {
                    Resource outSequence = registry.get(outSeqChildPath);
                    try {
                        OMElement seqElment = APIUtil.buildOMElement(outSequence.getContentStream());
                        sequenceList.add(seqElment.getAttributeValue(new QName("name")));
                    } catch (OMException e) {
                        log.info("Error occurred when reading the sequence '" + outSeqChildPath + "' from the registry.", e);
                    }
                }
            }
        }
    } catch (RegistryException e) {
        String msg = "Error while retrieving registry for tenant " + tenantId;
        log.error(msg);
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.registry.api.RegistryException e) {
        String msg = "Error while processing the " + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT + " sequences of " + apiIdentifier + " in the registry";
        log.error(msg);
        throw new APIManagementException(msg, e);
    } catch (Exception e) {
        log.error(e.getMessage());
        throw new APIManagementException(e.getMessage(), e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return new ArrayList<>(sequenceList);
}
Also used : QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) ArrayList(java.util.ArrayList) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) OMElement(org.apache.axiom.om.OMElement) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) UnsupportedPolicyTypeException(org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) OMException(org.apache.axiom.om.OMException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TreeSet(java.util.TreeSet) OMException(org.apache.axiom.om.OMException)

Example 42 with OMException

use of org.apache.axiom.om.OMException in project carbon-apimgt by wso2.

the class APIProviderImpl method getCustomInSequences.

/**
 * Get stored custom inSequences from governanceSystem registry
 *
 * @throws APIManagementException
 */
public List<String> getCustomInSequences(APIIdentifier apiIdentifier) throws APIManagementException {
    List<String> sequenceList = new ArrayList<String>();
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = null;
        if (apiIdentifier.getProviderName().contains("-AT-")) {
            String provider = apiIdentifier.getProviderName().replace("-AT-", "@");
            tenantDomain = MultitenantUtils.getTenantDomain(provider);
        }
        PrivilegedCarbonContext.startTenantFlow();
        isTenantFlowStarted = true;
        if (!StringUtils.isEmpty(tenantDomain)) {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        } else {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
        }
        UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
        if (registry.resourceExists(APIConstants.API_CUSTOM_INSEQUENCE_LOCATION)) {
            org.wso2.carbon.registry.api.Collection inSeqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(APIConstants.API_CUSTOM_INSEQUENCE_LOCATION);
            if (inSeqCollection != null) {
                String[] inSeqChildPaths = inSeqCollection.getChildren();
                Arrays.sort(inSeqChildPaths);
                for (String inSeqChildPath : inSeqChildPaths) {
                    Resource inSequence = registry.get(inSeqChildPath);
                    try {
                        OMElement seqElment = APIUtil.buildOMElement(inSequence.getContentStream());
                        sequenceList.add(seqElment.getAttributeValue(new QName("name")));
                    } catch (OMException e) {
                        log.info("Error occurred when reading the sequence '" + inSeqChildPath + "' from the registry.", e);
                    }
                }
            }
        }
        String customInSeqFileLocation = APIUtil.getSequencePath(apiIdentifier, "in");
        if (registry.resourceExists(customInSeqFileLocation)) {
            org.wso2.carbon.registry.api.Collection inSeqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(customInSeqFileLocation);
            if (inSeqCollection != null) {
                String[] inSeqChildPaths = inSeqCollection.getChildren();
                Arrays.sort(inSeqChildPaths);
                for (String inSeqChildPath : inSeqChildPaths) {
                    Resource inSequence = registry.get(inSeqChildPath);
                    try {
                        OMElement seqElment = APIUtil.buildOMElement(inSequence.getContentStream());
                        sequenceList.add(seqElment.getAttributeValue(new QName("name")));
                    } catch (OMException e) {
                        log.info("Error occurred when reading the sequence '" + inSeqChildPath + "' from the registry.", e);
                    }
                }
            }
        }
    } catch (Exception e) {
        handleException("Issue is in getting custom InSequences from the Registry", e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return sequenceList;
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) OMElement(org.apache.axiom.om.OMElement) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) UnsupportedPolicyTypeException(org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) OMException(org.apache.axiom.om.OMException) OMException(org.apache.axiom.om.OMException)

Example 43 with OMException

use of org.apache.axiom.om.OMException in project wso2-axis2-transports by wso2.

the class XMPPPacketListener method createSOAPEnvelopeForRawMessage.

/**
 * Creates a SOAP envelope using details found in chat message.
 * @param msgCtx
 * @param chatMessage
 * @return
 */
private SOAPEnvelope createSOAPEnvelopeForRawMessage(MessageContext msgCtx, String chatMessage) throws AxisFault {
    // TODO : need to add error handling logic
    String callRemoved = chatMessage.replaceFirst("call", "");
    // extract Service name
    String serviceName = callRemoved.trim().substring(0, callRemoved.indexOf(":") - 1);
    String operationName = callRemoved.trim().substring(callRemoved.indexOf(":"), callRemoved.indexOf("(") - 1);
    // Extract parameters from IM message
    String parameterList = callRemoved.trim().substring(callRemoved.indexOf("("), callRemoved.trim().length() - 1);
    StringTokenizer st = new StringTokenizer(parameterList, ",");
    MultipleEntryHashMap parameterMap = new MultipleEntryHashMap();
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        String name = token.substring(0, token.indexOf("="));
        String value = token.substring(token.indexOf("=") + 1);
        parameterMap.put(name, value);
    }
    SOAPEnvelope envelope = null;
    try {
        msgCtx.setProperty(XMPPConstants.CONTAINS_SOAP_ENVELOPE, new Boolean(true));
        if (serviceName != null && serviceName.trim().length() > 0) {
            AxisService axisService = msgCtx.getConfigurationContext().getAxisConfiguration().getService(serviceName);
            msgCtx.setAxisService(axisService);
            AxisOperation axisOperation = axisService.getOperationBySOAPAction("urn:" + operationName);
            if (axisOperation != null) {
                msgCtx.setAxisOperation(axisOperation);
            }
        }
        if (operationName != null && operationName.trim().length() > 0) {
            msgCtx.setSoapAction("urn:" + operationName);
        }
        XMPPOutTransportInfo xmppOutTransportInfo = (XMPPOutTransportInfo) msgCtx.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO);
        // This should be only set for messages received via chat.
        // TODO : need to read from a constant
        xmppOutTransportInfo.setContentType("xmpp/text");
        msgCtx.setServerSide(true);
        // TODO : need to support SOAP12 as well
        SOAPFactory soapFactory = new SOAP11Factory();
        envelope = BuilderUtil.buildsoapMessage(msgCtx, parameterMap, soapFactory);
    // TODO : improve error handling & messages
    } catch (AxisFault e) {
        throw new AxisFault(e.getMessage());
    } catch (OMException e) {
        throw new AxisFault(e.getMessage());
    } catch (FactoryConfigurationError e) {
        throw new AxisFault(e.getMessage());
    }
    return envelope;
}
Also used : AxisFault(org.apache.axis2.AxisFault) AxisOperation(org.apache.axis2.description.AxisOperation) AxisService(org.apache.axis2.description.AxisService) MultipleEntryHashMap(org.apache.axis2.util.MultipleEntryHashMap) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory) StringTokenizer(java.util.StringTokenizer) SOAP11Factory(org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory) OMException(org.apache.axiom.om.OMException) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError)

Example 44 with OMException

use of org.apache.axiom.om.OMException in project wso2-synapse by wso2.

the class JsonDataSource method serialize.

public void serialize(OutputStream output, OMOutputFormat format) throws XMLStreamException {
    try {
        if (format != null && format.getContentType() != null) {
            if (format.getContentType().contains("xml")) {
                // reuse the stream
                inputStream.reset();
                JsonUtil.toXml(inputStream, false).serialize(output, format);
                return;
            }
        }
        inputStream.reset();
        IOUtils.copy(inputStream, output);
    } catch (IOException e) {
        logger.error("#serialize:OutputStream. Could not serialize JSON payload. Error>>> " + e.getLocalizedMessage());
        throw new OMException("Could not serialize JSON payload.", e);
    }
}
Also used : OMException(org.apache.axiom.om.OMException)

Example 45 with OMException

use of org.apache.axiom.om.OMException in project wso2-synapse by wso2.

the class JsonDataSourceImpl method serialize.

public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
    XMLStreamReader reader = getReader();
    xmlWriter.writeStartDocument();
    while (reader.hasNext()) {
        int x = reader.next();
        switch(x) {
            case XMLStreamConstants.START_ELEMENT:
                xmlWriter.writeStartElement(reader.getPrefix(), reader.getLocalName(), reader.getNamespaceURI());
                int namespaceCount = reader.getNamespaceCount();
                for (int i = namespaceCount - 1; i >= 0; i--) {
                    xmlWriter.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
                }
                int attributeCount = reader.getAttributeCount();
                for (int i = 0; i < attributeCount; i++) {
                    xmlWriter.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i));
                }
                break;
            case XMLStreamConstants.START_DOCUMENT:
                break;
            case XMLStreamConstants.CHARACTERS:
                xmlWriter.writeCharacters(reader.getText());
                break;
            case XMLStreamConstants.CDATA:
                xmlWriter.writeCData(reader.getText());
                break;
            case XMLStreamConstants.END_ELEMENT:
                xmlWriter.writeEndElement();
                break;
            case XMLStreamConstants.END_DOCUMENT:
                xmlWriter.writeEndDocument();
                break;
            case XMLStreamConstants.SPACE:
                break;
            case XMLStreamConstants.COMMENT:
                xmlWriter.writeComment(reader.getText());
                break;
            case XMLStreamConstants.DTD:
                xmlWriter.writeDTD(reader.getText());
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                xmlWriter.writeProcessingInstruction(reader.getPITarget(), reader.getPIData());
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                xmlWriter.writeEntityRef(reader.getLocalName());
                break;
            default:
                throw new OMException();
        }
    }
    xmlWriter.writeEndDocument();
    xmlWriter.flush();
    xmlWriter.close();
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) OMException(org.apache.axiom.om.OMException)

Aggregations

OMException (org.apache.axiom.om.OMException)89 OMElement (org.apache.axiom.om.OMElement)35 XMLStreamException (javax.xml.stream.XMLStreamException)31 IOException (java.io.IOException)30 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)21 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)18 InputStream (java.io.InputStream)16 QName (javax.xml.namespace.QName)13 OMFactory (org.apache.axiom.om.OMFactory)12 ArrayList (java.util.ArrayList)11 MalformedURLException (java.net.MalformedURLException)10 Test (org.junit.Test)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)9 ParseException (org.json.simple.parser.ParseException)9 APIMgtResourceAlreadyExistsException (org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException)9 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)9 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)9 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)9 UnsupportedPolicyTypeException (org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException)9