use of org.apache.synapse.api.API in project wso2-synapse by wso2.
the class URITemplateBasedDispatcherTest method testBasicTemplateDispatch1.
public void testBasicTemplateDispatch1() throws Exception {
API api = new API("TestAPI", "/test");
Resource resource = new Resource();
resource.setDispatcherHelper(new URITemplateHelper("/~{user}"));
resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
api.addResource(resource);
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(api.getName(), api);
RESTRequestHandler handler = new RESTRequestHandler();
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/~foo", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
assertEquals("foo", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "user"));
synCtx = getMessageContext(synapseConfig, false, "/test/foo", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/test/~foo/bar", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(PROP_NAME));
}
use of org.apache.synapse.api.API in project wso2-synapse by wso2.
the class APISerializationTest method testAPISerialization2.
public void testAPISerialization2() throws Exception {
String xml = "<api name=\"test\" context=\"/dictionary\" binds-to=\"\" transports=\"https\" hostname=\"apache.org\" port=\"8243\"" + " xmlns=\"http://ws.apache.org/ns/synapse\"><resource binds-to=\"\" url-mapping=\"/admin/view\" " + "inSequence=\"in\" outSequence=\"out\"/></api>";
OMElement om = AXIOMUtil.stringToOM(xml);
API api = APIFactory.createAPI(om);
OMElement out = APISerializer.serializeAPI(api);
assertXMLEqual(xml, out.toString());
}
use of org.apache.synapse.api.API in project wso2-synapse by wso2.
the class APISerializationTest method testAPISerialization4.
public void testAPISerialization4() throws Exception {
String xml = "<api name=\"test\" context=\"/dictionary\" binds-to=\"\" transports=\"https\" hostname=\"apache.org\" port=\"8243\"" + " xmlns=\"http://ws.apache.org/ns/synapse\"><resource binds-to=\"\" url-mapping=\"/admin/view\" " + "outSequence=\"out\"><inSequence><log/><send/></inSequence></resource></api>";
OMElement om = AXIOMUtil.stringToOM(xml);
API api = APIFactory.createAPI(om);
OMElement out = APISerializer.serializeAPI(api);
assertXMLEqual(xml, out.toString());
}
use of org.apache.synapse.api.API in project wso2-synapse by wso2.
the class APISerializationTest method testAPISerialization5.
public void testAPISerialization5() throws Exception {
String xml = "<api xmlns=\"http://ws.apache.org/ns/synapse\" name=\"test\" context=\"/dictionary\" " + "hostname=\"apache.org\" port=\"8243\" binds-to=\"\" transports=\"https\">" + "<resource binds-to=\"\" url-mapping=\"/admin/view/*\" " + "><inSequence><log/><send/></inSequence><outSequence><log/><send/></outSequence></resource>" + "<resource binds-to=\"\" url-mapping=\"/admin/*\"><inSequence><log/><send/></inSequence><outSequence>" + "<log/><send/></outSequence></resource><resource binds-to=\"\" uri-template=\"/{char}/{word}\">" + "<inSequence><send/></inSequence><faultSequence><log level=\"full\"/></faultSequence>" + "</resource></api>";
OMElement om = AXIOMUtil.stringToOM(xml);
API api = APIFactory.createAPI(om);
OMElement out = APISerializer.serializeAPI(api);
assertXMLEqual(xml, out.toString());
}
use of org.apache.synapse.api.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()));
}
OMAttribute publishSwagger = apiElt.getAttribute(new QName("publishSwagger"));
if (publishSwagger != null) {
api.setSwaggerResourcePath(publishSwagger.getAttributeValue());
}
InboundApiUtils.addBindsTo(api, apiElt);
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();
}
}
}
CommentListUtil.populateComments(apiElt, api.getCommentsList());
return api;
}
Aggregations