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;
}
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);
}
}
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;
}
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;
}
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);
}
}
Aggregations