use of org.apache.synapse.rest.API in project wso2-synapse by wso2.
the class MultiXMLConfigurationBuilder method createAPIs.
private static void createAPIs(SynapseConfiguration synapseConfig, String rootDirPath, Properties properties) {
File apiDir = new File(rootDirPath, REST_API_DIR);
if (apiDir.exists()) {
if (log.isDebugEnabled()) {
log.debug("Loading APIs from :" + apiDir.getPath());
}
Iterator apiIterator = FileUtils.iterateFiles(apiDir, extensions, false);
while (apiIterator.hasNext()) {
File file = (File) apiIterator.next();
try {
OMElement document = getOMElement(file);
API api = SynapseXMLConfigurationFactory.defineAPI(synapseConfig, document, properties, false);
if (api != null) {
api.setFileName(file.getName());
synapseConfig.getArtifactDeploymentStore().addArtifact(file.getAbsolutePath(), api.getName());
}
} catch (Exception e) {
String msg = "API configuration cannot be built from : " + file.getName();
handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_API, msg, e);
}
}
// order the apis based on context descending order
try {
SynapseXMLConfigurationFactory.reOrderAPIs(synapseConfig);
} catch (Exception e) {
String msg = "Error while re-ordering apis";
handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_API, msg, e);
}
}
}
use of org.apache.synapse.rest.API in project wso2-synapse by wso2.
the class MultiXMLConfigurationSerializer method serializeSynapseXML.
/**
* Serialize only the elements defined in the top level synapse.xml file back to the
* synapse.xml file. This method ignores the elements defined in files other than the
* synapse.xml. Can be used in situations where only the synapse.xml file should be
* updated at runtime.
*
* @param synapseConfig Current Synapse configuration
* @throws Exception on file I/O error
*/
public void serializeSynapseXML(SynapseConfiguration synapseConfig) throws Exception {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMElement definitions = fac.createOMElement("definitions", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
if (synapseConfig.getRegistry() != null && !Boolean.valueOf(synapseConfig.getProperty(MultiXMLConfigurationBuilder.SEPARATE_REGISTRY_DEFINITION))) {
RegistrySerializer.serializeRegistry(definitions, synapseConfig.getRegistry());
}
if (synapseConfig.getTaskManager() != null && !Boolean.valueOf(synapseConfig.getProperty(MultiXMLConfigurationBuilder.SEPARATE_TASK_MANAGER_DEFINITION))) {
TaskManagerSerializer.serializetaskManager(definitions, synapseConfig.getTaskManager());
}
Collection<ProxyService> proxyServices = synapseConfig.getProxyServices();
Collection<SynapseEventSource> eventSources = synapseConfig.getEventSources();
Collection<Startup> tasks = synapseConfig.getStartups();
Collection localEntries = synapseConfig.getLocalRegistry().values();
Collection<PriorityExecutor> executors = synapseConfig.getPriorityExecutors().values();
Collection<MessageStore> messageStores = synapseConfig.getMessageStores().values();
Collection<MessageProcessor> messageProcessors = synapseConfig.getMessageProcessors().values();
Collection<API> apiCollection = synapseConfig.getAPIs();
Collection<SynapseImport> synapseImportsCollection = synapseConfig.getSynapseImports().values();
Collection<InboundEndpoint> inboundEndpoints = synapseConfig.getInboundEndpoints();
Collection<String> comments = synapseConfig.getCommentedTextList();
for (ProxyService service : proxyServices) {
if (service.getFileName() == null) {
ProxyServiceSerializer.serializeProxy(definitions, service);
}
}
for (SynapseEventSource source : eventSources) {
if (source.getFileName() == null) {
EventSourceSerializer.serializeEventSource(definitions, source);
}
}
for (Startup task : tasks) {
if (task instanceof AbstractStartup && task.getFileName() == null) {
StartupFinder.getInstance().serializeStartup(definitions, task);
}
}
for (Object o : localEntries) {
if (o instanceof TemplateMediator) {
TemplateMediator template = (TemplateMediator) o;
if (template.getFileName() == null) {
MediatorSerializerFinder.getInstance().getSerializer(template).serializeMediator(definitions, template);
}
} else if (o instanceof SequenceMediator) {
SequenceMediator seq = (SequenceMediator) o;
if (seq.getFileName() == null) {
MediatorSerializerFinder.getInstance().getSerializer(seq).serializeMediator(definitions, seq);
}
} else if (o instanceof Template) {
Template templEndpoint = (Template) o;
if (templEndpoint.getFileName() == null) {
new TemplateSerializer().serializeEndpointTemplate(templEndpoint, definitions);
}
} else if (o instanceof AbstractEndpoint) {
AbstractEndpoint endpoint = (AbstractEndpoint) o;
if (endpoint.getFileName() == null) {
OMElement endpointElem = EndpointSerializer.getElementFromEndpoint(endpoint);
definitions.addChild(endpointElem);
}
} else if (o instanceof Entry) {
Entry entry = (Entry) o;
if (entry.getFileName() == null) {
if ((SynapseConstants.SERVER_HOST.equals(entry.getKey()) || SynapseConstants.SERVER_IP.equals(entry.getKey())) || entry.getType() == Entry.REMOTE_ENTRY) {
continue;
}
EntrySerializer.serializeEntry(entry, definitions);
}
}
}
for (PriorityExecutor executor : executors) {
PriorityExecutorSerializer.serialize(definitions, executor, SynapseConstants.SYNAPSE_NAMESPACE);
}
for (MessageStore messageStore : messageStores) {
if (messageStore.getFileName() == null) {
MessageStoreSerializer.serializeMessageStore(definitions, messageStore);
}
}
for (MessageProcessor messageProcessor : messageProcessors) {
if (messageProcessor.getFileName() == null) {
MessageProcessorSerializer.serializeMessageProcessor(definitions, messageProcessor);
}
}
for (API api : apiCollection) {
if (api.getFileName() == null) {
APISerializer.serializeAPI(definitions, api);
}
}
for (SynapseImport synapseImport : synapseImportsCollection) {
if (synapseImport.getFileName() == null) {
SynapseImportSerializer.serializeImport(definitions, synapseImport);
}
}
for (InboundEndpoint inboundEndpoint : inboundEndpoints) {
if (inboundEndpoint.getFileName() == null) {
InboundEndpointSerializer.serializeInboundEndpoint(definitions, inboundEndpoint);
}
}
serializeComments(comments, definitions);
serializeSynapseXML(definitions);
}
use of org.apache.synapse.rest.API in project wso2-synapse by wso2.
the class APIDebugUtil method registerAPISequenceMediationFlowBreakPoint.
/**
* Registers/Un-registers a breakpoint, point where mediation flow get suspended
*
* @param synCfg Synapse configuration
* @param mapping either resource url-mapping or uri-template
* @param method resource http method
* @param sequenceType Synapse sequence type
* @param apiKey name of the API
* @param position array of integers that uniquely specifies a point in mediation route
* @param registerMode specify whether register or un register
*/
public static void registerAPISequenceMediationFlowBreakPoint(SynapseConfiguration synCfg, String mapping, String method, String sequenceType, String apiKey, int[] position, boolean registerMode) {
SynapseSequenceType synapseSequenceType = SynapseSequenceType.valueOf(sequenceType.toUpperCase());
APIMediationFlowPoint breakPoint = new APIMediationFlowPoint();
breakPoint.setSynapseMediationComponent(SynapseMediationComponent.SEQUENCE);
breakPoint.setKey(apiKey);
breakPoint.setMediatorPosition(position);
breakPoint.setSynapseSequenceType(synapseSequenceType);
breakPoint.setSequenceBaseType(SynapseDebugCommandConstants.DEBUG_COMMAND_MEDIATION_COMPONENT_SEQUENCE_API);
breakPoint.setResourceMapping(mapping);
breakPoint.setResourceHTTPMethod(method);
Mediator seqMediator = null;
Resource api_resource = null;
API api = synCfg.getAPI(apiKey);
if (api == null) {
if (log.isDebugEnabled()) {
log.debug("Non existing API for the key " + breakPoint.getKey());
}
debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_API_NOT_FOUND).toString());
return;
}
Resource[] resource_array = api.getResources();
for (int counter = 0; counter < resource_array.length; counter++) {
if (resource_array[counter].getDispatcherHelper() != null && mapping != null) {
if (mapping.equals(resource_array[counter].getDispatcherHelper().getString())) {
for (String m1 : resource_array[counter].getMethods()) {
if (m1.equals(method)) {
api_resource = resource_array[counter];
break;
}
}
if (api_resource != null) {
break;
}
}
} else if (resource_array[counter].getDispatcherHelper() == null && mapping == null) {
for (String m1 : resource_array[counter].getMethods()) {
if (m1.equals(method)) {
api_resource = resource_array[counter];
break;
}
}
if (api_resource != null) {
break;
}
}
}
if (api_resource != null) {
if (synapseSequenceType.equals(SynapseSequenceType.API_INSEQ)) {
seqMediator = api_resource.getInSequence();
} else if (synapseSequenceType.equals(SynapseSequenceType.API_OUTSEQ)) {
seqMediator = api_resource.getOutSequence();
} else if (synapseSequenceType.equals(SynapseSequenceType.API_FAULTSEQ)) {
seqMediator = api_resource.getFaultSequence();
}
} else {
if (log.isDebugEnabled()) {
log.debug("Resource not found for the API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod());
}
debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_API_RESOURCE_NOT_FOUND).toString());
return;
}
if (seqMediator != null) {
Mediator current_mediator = null;
current_mediator = MediatorTreeTraverseUtil.getMediatorReference(synCfg, seqMediator, position);
if (current_mediator != null) {
breakPoint.setMediatorReference(current_mediator);
if (registerMode) {
if (!((AbstractMediator) current_mediator).isBreakPoint()) {
((AbstractMediator) current_mediator).setBreakPoint(true);
((AbstractMediator) current_mediator).registerMediationFlowPoint(breakPoint);
if (log.isDebugEnabled()) {
log.info("Registered breakpoint at mediator position " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
}
debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(true, null).toString());
} else {
if (log.isDebugEnabled()) {
log.debug("Failed register breakpoint. Already breakpoint enabled at mediator position " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
}
debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_ALREADY_BREAKPOINT_ENABLED).toString());
}
} else {
if (((AbstractMediator) current_mediator).isBreakPoint()) {
((AbstractMediator) current_mediator).unregisterMediationFlowPoint();
((AbstractMediator) current_mediator).setBreakPoint(false);
if (log.isDebugEnabled()) {
log.debug("Unregistered breakpoint at mediator position " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
}
debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(true, null).toString());
} else {
if (log.isDebugEnabled()) {
log.debug("Failed unregister breakpoint. Already breakpoint disabled at mediator position " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
}
debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_ALREADY_BREAKPOINT_DISABLED).toString());
}
}
} else {
if (registerMode) {
if (log.isDebugEnabled()) {
log.debug("Failed register breakpoint. Non existing mediator position at " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
}
debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_MEDIATOR_POSITION).toString());
} else {
if (log.isDebugEnabled()) {
log.debug("Failed unregister breakpoint. Non existing mediator position at " + logMediatorPosition(breakPoint) + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod() + " sequence type " + breakPoint.getSynapseSequenceType().toString());
}
debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_MEDIATOR_POSITION).toString());
}
}
} else {
if (registerMode) {
if (log.isDebugEnabled()) {
log.debug("Failed register breakpoint. Non existing sequence " + breakPoint.getSynapseSequenceType().toString() + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod());
}
debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_SEQUENCE).toString());
} else {
if (log.isDebugEnabled()) {
log.debug("Failed unregister breakpoint. Non existing sequence " + breakPoint.getSynapseSequenceType().toString() + " for API key " + breakPoint.getKey() + " resource mapping " + breakPoint.getResourceMapping() + " resource HTTP method " + breakPoint.getResourceHTTPMethod());
}
debugManager.advertiseCommandResponse(debugManager.createDebugCommandResponse(false, SynapseDebugCommandConstants.DEBUG_COMMAND_RESPONSE_NON_EXISTING_SEQUENCE).toString());
}
}
}
use of org.apache.synapse.rest.API in project wso2-synapse by wso2.
the class ContinuationStackManager method pushRootFaultHandlerForSequence.
/**
* Find the correct root fault handler for named sequences.
*
* If the message is initiated from a proxy, we need to assign the proxy fault sequence.
* If the message is initiated from a API Resource, we need to assign the resource fault sequence.
*
* @param synCtx message context
*/
private static void pushRootFaultHandlerForSequence(MessageContext synCtx) {
// For Proxy services
String proxyName = (String) synCtx.getProperty(SynapseConstants.PROXY_SERVICE);
if (proxyName != null && !"".equals(proxyName)) {
ProxyService proxyService = synCtx.getConfiguration().getProxyService(proxyName);
if (proxyService != null) {
proxyService.registerFaultHandler(synCtx);
} else {
handleException("Proxy service : " + proxyName + " not found");
}
return;
}
// For APIs
String apiName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API);
if (apiName != null && !"".equals(apiName)) {
API api = synCtx.getEnvironment().getSynapseConfiguration().getAPI(apiName);
if (api != null) {
String resourceName = (String) synCtx.getProperty(RESTConstants.SYNAPSE_RESOURCE);
Resource resource = api.getResource(resourceName);
if (resource != null) {
resource.registerFaultHandler(synCtx);
} else {
handleException("Resource : " + resourceName + " not found");
}
} else {
handleException("REST API : " + apiName + " not found");
}
return;
}
// For main sequence/MessageInjector etc, push the default fault handler
synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getFaultSequence()));
}
use of org.apache.synapse.rest.API in project wso2-synapse by wso2.
the class APIFactory method createAPI.
public static API createAPI(OMElement apiElt, Properties properties) {
OMAttribute nameAtt = apiElt.getAttribute(new QName("name"));
if (nameAtt == null || "".equals(nameAtt.getAttributeValue())) {
handleException("Attribute 'name' is required for an API definition");
}
OMAttribute contextAtt = apiElt.getAttribute(new QName("context"));
if (contextAtt == null || "".equals(contextAtt.getAttributeValue())) {
handleException("Attribute 'context' is required for an API definition");
}
API api = new API(nameAtt.getAttributeValue(), contextAtt.getAttributeValue());
OMAttribute hostAtt = apiElt.getAttribute(new QName("hostname"));
if (hostAtt != null && !"".equals(hostAtt.getAttributeValue())) {
api.setHost(hostAtt.getAttributeValue());
}
VersionStrategy vStrategy = VersionStrategyFactory.createVersioningStrategy(api, apiElt);
api.setVersionStrategy(vStrategy);
OMAttribute portAtt = apiElt.getAttribute(new QName("port"));
if (portAtt != null && !"".equals(portAtt.getAttributeValue())) {
api.setPort(Integer.parseInt(portAtt.getAttributeValue()));
}
Iterator resources = apiElt.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "resource"));
boolean noResources = true;
while (resources.hasNext()) {
OMElement resourceElt = (OMElement) resources.next();
api.addResource(ResourceFactory.createResource(resourceElt, properties));
noResources = false;
}
if (noResources) {
handleException("An API must contain at least one resource definition");
}
OMElement handlersElt = apiElt.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "handlers"));
if (handlersElt != null) {
Iterator handlers = handlersElt.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "handler"));
while (handlers.hasNext()) {
OMElement handlerElt = (OMElement) handlers.next();
defineHandler(api, handlerElt);
}
}
OMAttribute trans = apiElt.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "transports"));
if (trans != null) {
String transports = trans.getAttributeValue();
if (!"".equals(transports)) {
if (Constants.TRANSPORT_HTTP.equals(transports)) {
api.setProtocol(RESTConstants.PROTOCOL_HTTP_ONLY);
} else if (Constants.TRANSPORT_HTTPS.equals(transports)) {
api.setProtocol(RESTConstants.PROTOCOL_HTTPS_ONLY);
} else {
handleException("Invalid protocol name: " + transports);
}
}
}
String nameString = api.getName();
if (nameString == null || "".equals(nameString)) {
nameString = SynapseConstants.ANONYMOUS_API;
}
AspectConfiguration aspectConfiguration = new AspectConfiguration(nameString);
api.configure(aspectConfiguration);
OMAttribute statistics = apiElt.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.STATISTICS_ATTRIB_NAME));
if (statistics != null) {
String statisticsValue = statistics.getAttributeValue();
if (statisticsValue != null) {
if (XMLConfigConstants.STATISTICS_ENABLE.equals(statisticsValue)) {
aspectConfiguration.enableStatistics();
}
}
}
OMAttribute tracing = apiElt.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.TRACE_ATTRIB_NAME));
if (tracing != null) {
String tracingValue = tracing.getAttributeValue();
if (tracingValue != null) {
if (XMLConfigConstants.TRACE_ENABLE.equals(tracingValue)) {
aspectConfiguration.enableTracing();
}
}
}
return api;
}
Aggregations