Search in sources :

Example 21 with ApiException

use of org.wso2.carbon.identity.mgt.endpoint.util.client.ApiException in project carbon-mediation by wso2.

the class RestApiAdmin method getServerContext.

public String getServerContext() throws APIException {
    AxisConfiguration configuration = null;
    try {
        configuration = ConfigHolder.getInstance().getAxisConfiguration();
    } catch (APIException e) {
        handleException(log, "Could not retrieve server context", e);
    }
    String portValue;
    String protocol;
    TransportInDescription transportInDescription = configuration.getTransportIn("http");
    if (transportInDescription == null) {
        transportInDescription = configuration.getTransportIn("https");
    }
    if (transportInDescription != null) {
        protocol = transportInDescription.getName();
        portValue = (String) transportInDescription.getParameter("port").getValue();
    } else {
        throw new APIException("http/https transport required");
    }
    String host;
    Parameter hostParam = configuration.getParameter("hostname");
    if (hostParam != null) {
        host = (String) hostParam.getValue();
    } else {
        try {
            host = NetworkUtils.getLocalHostname();
        } catch (SocketException e) {
            log.warn("SocketException occured when trying to obtain IP address of local machine");
            host = "localhost";
        }
    }
    String serverContext = "";
    try {
        int port = Integer.parseInt(portValue);
        if ("http".equals(protocol) && port == 80) {
            port = -1;
        } else if ("https".equals(protocol) && port == 443) {
            port = -1;
        }
        URL serverURL = new URL(protocol, host, port, "");
        serverContext = serverURL.toExternalForm();
    } catch (MalformedURLException e) {
        handleException(log, "Error when generating server context URL", e);
    } catch (NumberFormatException e) {
        handleException(log, "Error when getting the port for server context URL", e);
    }
    return serverContext;
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) APIException(org.wso2.carbon.rest.api.APIException) Parameter(org.apache.axis2.description.Parameter) TransportInDescription(org.apache.axis2.description.TransportInDescription) URL(java.net.URL)

Example 22 with ApiException

use of org.wso2.carbon.identity.mgt.endpoint.util.client.ApiException in project carbon-mediation by wso2.

the class RestApiAdmin method addApi.

/**
 * Add an api described by the given OMElement
 *
 * @param apiElement configuration of the api which needs to be added
 * @param fileName Name of the file in which this configuration should be saved or null
 * @throws APIException if the element is not an api or if an api with the
 *                   same name exists
 */
private void addApi(OMElement apiElement, String fileName, boolean updateMode) throws APIException {
    try {
        if (apiElement.getQName().getLocalPart().equals(XMLConfigConstants.API_ELT.getLocalPart())) {
            String apiName = apiElement.getAttributeValue(new QName("name"));
            String apiTransports = apiElement.getAttributeValue(new QName("transports"));
            if (getSynapseConfiguration().getAxisConfiguration().getService(apiName) != null) {
                handleException(log, "A service named " + apiName + " already exists", null);
            } else {
                Properties properties = new Properties();
                properties.put(SYNAPSE_CONFIGURATION, getSynapseConfiguration());
                API api = APIFactory.createAPI(apiElement, properties);
                try {
                    getSynapseConfiguration().addAPI(api.getName(), api);
                    if (log.isDebugEnabled()) {
                        log.debug("Added API : " + apiName);
                        log.debug("Authorized Transports : " + apiTransports);
                    }
                    if (apiTransports != null) {
                        if (Constants.TRANSPORT_HTTP.equalsIgnoreCase(apiTransports)) {
                            api.setProtocol(RESTConstants.PROTOCOL_HTTP_ONLY);
                        } else if (Constants.TRANSPORT_HTTPS.equalsIgnoreCase(apiTransports)) {
                            api.setProtocol(RESTConstants.PROTOCOL_HTTPS_ONLY);
                        }
                    }
                    if (updateMode) {
                        api.setFileName(fileName);
                    } else {
                        if (fileName != null) {
                            api.setFileName(fileName);
                        } else {
                            api.setFileName(ServiceBusUtils.generateFileName(api.getName()));
                        }
                    }
                    api.init(getSynapseEnvironment());
                    persistApi(api);
                } catch (Exception e) {
                    api.destroy();
                    getSynapseConfiguration().removeAPI(api.getName());
                    try {
                        if (getAxisConfig().getService(api.getName()) != null) {
                            getAxisConfig().removeService(api.getName());
                        }
                    } catch (Exception ignore) {
                    }
                    handleException(log, "Error trying to add the API to the ESB " + "configuration : " + api.getName(), e);
                }
            }
        } else {
            handleException(log, "Invalid API definition", null);
        }
    } catch (AxisFault af) {
        handleException(log, "Invalid API definition", af);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) QName(javax.xml.namespace.QName) API(org.apache.synapse.api.API) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIException(org.wso2.carbon.rest.api.APIException) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) APIGenException(org.wso2.carbon.mediation.commons.rest.api.swagger.APIGenException)

Example 23 with ApiException

use of org.wso2.carbon.identity.mgt.endpoint.util.client.ApiException in project carbon-mediation by wso2.

the class RestApiAdmin method getSwaggerDocument.

/**
 * Return the registry resource for the provided location
 *
 * @param apiName
 * @param tenantId
 * @return
 * @throws APIException
 */
public String getSwaggerDocument(String apiName, int tenantId) throws APIException {
    API api = getSynapseAPIByName(apiName);
    if (api == null) {
        throw new APIException("API with name \"" + apiName + "\" does not exists.");
    }
    String resourcePath = getSwaggerResourcePath(api);
    String swaggerJsonString = null;
    if (resourcePath.startsWith(CONFIG_REG_PREFIX) || resourcePath.startsWith(GOV_REG_PREFIX)) {
        try {
            swaggerJsonString = getResourceFromRegistry(resourcePath, tenantId);
        } catch (RegistryException e) {
            handleException(log, "Could not get swagger document", e);
        }
    } else {
        // Read from URI
        try {
            swaggerJsonString = readFromURI(resourcePath);
        } catch (IOException e) {
            log.error("Error occurred while reading swagger definition from: " + resourcePath, e);
        }
    }
    // Generate if not available
    if (swaggerJsonString == null) {
        if (log.isDebugEnabled()) {
            log.debug("Generate swagger definition for the API : " + apiName);
        }
        swaggerJsonString = generateSwaggerFromSynapseAPI(api);
    }
    return swaggerJsonString;
}
Also used : APIException(org.wso2.carbon.rest.api.APIException) API(org.apache.synapse.api.API) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 24 with ApiException

use of org.wso2.carbon.identity.mgt.endpoint.util.client.ApiException in project carbon-mediation by wso2.

the class RestApiAdmin method deleteApi.

public boolean deleteApi(String apiName) throws APIException {
    final Lock lock = getLock();
    try {
        lock.lock();
        assertNameNotEmpty(apiName);
        apiName = apiName.trim();
        SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
        API api = synapseConfiguration.getAPI(apiName);
        if (api.getArtifactContainerName() == null) {
            if (log.isDebugEnabled()) {
                log.debug("Deleting API : " + apiName + " from the configuration");
            }
            api.destroy();
            synapseConfiguration.removeAPI(apiName);
            if (!Boolean.parseBoolean(System.getProperty("NonRegistryMode")) && saveRuntimeArtifacts) {
                MediationPersistenceManager pm = getMediationPersistenceManager();
                String fileName = api.getFileName();
                pm.deleteItem(apiName, fileName, ServiceBusConstants.ITEM_TYPE_REST_API);
            }
            if (log.isDebugEnabled()) {
                log.debug("Api : " + apiName + " removed from the configuration");
            }
        }
    } finally {
        lock.unlock();
    }
    return true;
}
Also used : MediationPersistenceManager(org.wso2.carbon.mediation.initializer.persistence.MediationPersistenceManager) API(org.apache.synapse.api.API) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Lock(java.util.concurrent.locks.Lock)

Example 25 with ApiException

use of org.wso2.carbon.identity.mgt.endpoint.util.client.ApiException in project carbon-mediation by wso2.

the class RestApiAdmin method handleException.

private void handleException(Log log, String message, Exception e) throws APIException {
    if (e == null) {
        APIException apiException = new APIException(message);
        log.error(message, apiException);
        throw apiException;
    } else {
        message = message + " :: " + e.getMessage();
        log.error(message, e);
        throw new APIException(message, e);
    }
}
Also used : APIException(org.wso2.carbon.rest.api.APIException)

Aggregations

API (org.apache.synapse.api.API)16 Lock (java.util.concurrent.locks.Lock)10 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)10 APIException (org.wso2.carbon.rest.api.APIException)9 APIGenException (org.wso2.carbon.mediation.commons.rest.api.swagger.APIGenException)8 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 XMLStreamException (javax.xml.stream.XMLStreamException)7 ApiException (org.wso2.carbon.identity.mgt.endpoint.util.client.ApiException)7 IOException (java.io.IOException)6 MalformedURLException (java.net.MalformedURLException)6 SocketException (java.net.SocketException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Pair (org.wso2.carbon.identity.mgt.endpoint.util.client.Pair)6 Property (org.wso2.carbon.identity.mgt.endpoint.util.client.model.Property)6 MediationPersistenceManager (org.wso2.carbon.mediation.initializer.persistence.MediationPersistenceManager)6 GenericType (com.sun.jersey.api.client.GenericType)4 AspectConfiguration (org.apache.synapse.aspects.AspectConfiguration)4 QName (javax.xml.namespace.QName)3