Get POST request data from filter in Enonic - content-management-system

Are there any ways to use one method for all post requests instead of having controllers for each post request? Can we use HTTP filters in Enonic to support POST requests? They support GET requests by default.

Yes, you can use "Mappings" in the Site descriptor to intercept e.g. a path.
<site>
<mappings>
<mapping controller="/site/foobar/api.js" order="10">
<pattern>/api/v\d+/.*</pattern>
</mapping>
</mappings>
</site>

Related

Struts2 - Best way to redirect to another domain

My website has a section that will be migrated to another domain, but all our clients still use the old url, which will be deprecated soon.
What i want to achieve is send 301 moved permanently HTTP Response, and send the users to the new domain.
Already tried
sendRedirect
ServletActionContext.getResponse().setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
ServletActionContext.getResponse().sendRedirect(urlToNewDomain);
and
ServletActionContext.getResponse().setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
ServletActionContext.getResponse().setHeader("Location", urlToNewDomain);
ServletActionContext.getResponse().setHeader("Connection", "close");
and with struts response (using XML)
<result name="externalRedirect" type="httpheader">
<param name="status">301</param>
<param name="headers.location">${targetUrl}</param>
</result>
The response its a 301 like: myDomain.com/newDomainUrl
So it keeps using myDomain.com ... i don't know how t explicitly redirect to a different domain.
Which is the best way to redirect to a different domain?

WSO2 API Manager convert SOAP to REST

Is it possible to publish a SOAP service as a REST API directly in the API manager? Is it possible to convert the call and expose REST to end user while calling the SOAP?
If possible, how?
Thanks.
This might be what you are looking for. This has can be done as mentioned below.
If you want to expose multiple operations using the same API in a RESTful manner you can modify the sequence in the post using the following guidelines.
1) Create a request URI to map to each operation in your backend SOAP service when designing the REST API in API Manager.
2) Using the filter mediator (which acts as a conditional statement in programming) you can filter out from the request URI(operation) and construct the required payload accordingly.
The below block would be repeated corresponding to your various operations mapping your backend web service.
The logic here would be if the request URI of the API is X route to operation Y of the SOAP service.
<!-- this filters out the operations of your API -->
<property expression="json-eval($.operation)" name="operation" />
<filter regex="menu" source="$ctx:operation">
<header description="SOAPAction" name="SOAPAction" scope="transport" value="http://ws.cdyne.com/PhoneVerify/query/CheckPhoneNumber"/>
<!-- We are storing the input values which the end users input for these values into properties -->
<property name="uri.var.phoneNumber" expression="$url:PhoneNumber"/>
<property name="uri.var.licenseKey" expression="$url:LicenseKey"/>
<!-- Since we do not want the URL pattern we mentioned to be sent to the backend we need to add the below property to remove it -->
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<!-- Now we need to create the actual payload which the backend requires. For that we use the payload factory mediator -->
<payloadFactory description="transform" media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:quer="http://ws.cdyne.com/PhoneVerify/query">
<soapenv:Header/>
<soapenv:Body>
<quer:CheckPhoneNumber>
<quer:PhoneNumber>$1</quer:PhoneNumber>
<quer:LicenseKey>$2</quer:LicenseKey>
</quer:CheckPhoneNumber></soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg expression="get-property(‘uri.var.phoneNumber’)"/>
<arg expression="get-property(‘uri.var.licenseKey’)"/>
</args>
</payloadFactory>
For further information on the above use case you can you can refer this post as reference on how such a custom extension sequence has been used to map a backend SOAP web service operations. With this you would be able to directly expose it as a REST API
Or you can simply create an SOAP based API in the WSO2 API Cloud or WSO2 API Manager and then pass the request payload along with the SOAP operation sent in the SOAP Action header so that you can call the different operations of your backend web service. You can see how this is used in the image below.
Managing WSDL operations using a single API
Hope this helps.
Regards.
yes. You can refer this blog post as reference. please note that there may be some differences as this was written for API manager Alpha version. Yet it is a good entry point.

Sending the post method contents in REST invocation from WSO2 ESB

I have been trying to invoke a rest operation from my wso2 ESB and was successful in invoking the rest post method from WSO2 ESB. But, un luckily i was not able to access the data that i posted, neither through request parameters nor through request attributes.
PS: I don't want to frame a get kind of URLs for my post request.
is there a solution to this ?
You need to use the correct content type so it will preserve POST request data.This post will help you to understand the reason.
Edit.
1) Add the following entry to axis2.xml in message builders.
<messageBuilder contentType="application/x-www-form-urlencoded"
class="org.apache.synapse.commons.builders.XFormURLEncodedBuilder"/>
2) then access the required parameter in esb using
<property name="NameOfTheProperty" expression="//xformValues/NameOfTheProperty/text()"/>

How to invoke RESTful services from HTML forms?

We're using Grails for building RESTful services which we'll call from browser clients using HTML forms, the problem is that forms only support GET and POST, so we're not sure how to handle PUT and DELETE.
Grails template tags can help you there:
However, issuing a request with a method other than GET or POST from a regular browser is not possible without some help from Grails. When defining a form you can specify an alternative method such as DELETE:
<g:form controller="book" method="DELETE">
..
</g:form>
Grails will send a hidden parameter called _method, which will be used as the request's HTTP method. Another alternative for changing the method for non-browser clients is to use the X-HTTP-Method-Override to specify the alternative method name.
Via: http://www.grails.org/doc/latest/guide/13.%20Web%20Services.html

How should I update a REST resource?

I'm not sure how I should go about updating individual properties of a REST resource. Consider the following example:
# HTTP GET to /users/1.xml
<?xml version="1.0" encoding="UTF-8" ?>
<response>
<user>
<id>1</id>
<name>John Doe</name>
<email>john#doe.com</email>
</user>
</response>
How should I facilitate for updating John's email? HTTP PUT comes to mind, but I'd be making it hard on my clients by requiring a complete XML (matching the HTTP GET response) to modify the resource.
The PUT method requests that the
enclosed entity be stored under the
supplied Request-URI. If the
Request-URI refers to an already
existing resource, the enclosed entity
SHOULD be considered as a modified
version of the one residing on the
origin server.
Is there any other way?
If your server framework is flexible enough to handle it, you can do:
Request:
PUT /users/1/email
Content-Type: text/plain
john#newemail.com
Response:
200 OK
Content-Location: /users/1
By using a URL to refer to the email as its own resource, you can PUT directly to it using a simple format like text/plain. In the response, the Content-Location url gives the client an indication that the change has had an impact on the user resource.
The PATCH method is also another way that you can do partial updates. This is a newly introduced method and as yet there are no standard formats for sending XML diff documents. So, if you take this approach you will not find much guidance.
The other thing to consider is that REST works best with large grained updates. If you find yourself needing to make these kinds of small changes, then maybe you need to rethink your distributed architecture.