AEM - Post CQ dynamic forms to external system using web service - java

We are building a site using AEM, where we have lot of forms (login, change address, change email) that will POST their data to an external system using web service. We had achieved this by using "slingServlet", where we used to get values using “sling.getParameter(“ ”). With this approach we are fixed with the form components used inside the particular forms.
For example login form, we are using username and password parameter, but in future we decided to add “dob” field too. To achieve this we need to do the code changes again. By doing so I believed we are not leveraging the CMS concept. So the question is there any other way to achieve this without doing code changes.

Instead of getting parameters by using sling.getParameter("") try using one of these instead
sling.getRequestParameterMap();
or
sling.getParamterMap();
This will give you a map of your parameters regardless of their name. Returns the parameters and name | value pairs.
Here are some helpful links:
https://sling.apache.org/apidocs/sling5/org/apache/sling/api/SlingHttpServletRequest.html#getRequestParameterMap()
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterMap()

Related

Watson Conversation Java SDK how to get from ExportEntity to CreateEntity?

We are trying to automate some tasks in the chatbot/conversation creation process.
A step in this automation is to take an existing conversation (intents, entities en dialogs) and this to a newly created Conversation.
While working with the API, I see that getting a workspace (https://www.ibm.com/watson/developercloud/conversation/api/v1/java#get_workspace )
returns different types EntityExport, IntentExport etc...
(http://watson-developer-cloud.github.io/java-sdk/docs/java-sdk-4.2.0/com/ibm/watson/developer_cloud/conversation/v1/model/EntityExport.html )
than what the UpdateWorkspace expects:
CreateEntity, CreateIntent etc...
(http://watson-developer-cloud.github.io/java-sdk/docs/java-sdk-4.2.0/com/ibm/watson/developer_cloud/conversation/v1/model/CreateEntity.html)
Before I start writing a copyTo function, I thought I would ask for any pitfalls ? There must be a reason why the objects retrieved via GET are different from the objects you need provide for an update/create ?
These classes are generated to match parameters of REST API endpoints.
It would be much simpler to use HTTP client to fetch JSON of the workspace, remove a few unnecessary attributes (workspace_id, status, created, updated, etc) and send it to create or update endpoint.

How to add a flag to existing POST api

I have a REST api built for creating orders. The behavior is such that the person who creates an order received an email back saying "You created an order XXX". This email is triggered all the time.
The api appears like this
http://api.mytestevnt.com/ordering/orders - POST with request body as the order entity json.
Now i want to give a feature to the api caller to indicate if the email notification is necessary or not. What's the best way to do this?
I think it depends on whether email notification is data or metadata. If it's part of the order, then definitely add it to the request body. If it's metadata, you have two choices. If you think there will be lots of metadata, you can either edit the order to have a metadata section or you can POST the metadata separately. If there will only be a limited amount of metadata, I would suggest using a query parameter.
You should avoid using a header unless you control the entire path from the client to the server, because proxies or load balancers are allowed to strip non-standard headers.
Include in the POST body a send_email=1 or send_email=0 param. You'll extract that, and see what the user wants to do.
Search "how to get POST variables in JAVA".
Accessing post variables using Java Servlets
You can do like this:
Add a new Java attribute(like boolean emailEnabled) in your Java Request Object for your REST service.
Client side which invokes your REST service need to provide that parameter you added in your server side, you can set a default value for that too.

Programmatically sending mule message (formerly: Accessing mule http endpoint from java)

Scroll towards the end for the solution to the topic's problem. The original question was asking for a somewhat different thing.
As a part of a larger process, I need to fetch and link two related sets of data together. The way that the data is retrieved(dynamics crm, n:n relationships..) forces us retrieve the second set of the data again so that it will have all the necessary information. During a part of larger transformation of this data, I would like to access the http endpoint that is used to fetch the data from the crm, retrieve the second set of data and process it. I can get the endpoint through DefaultEndPointFactory like so:
DefaultEndpointFactory def = new DefaultEndpointFactory();
def.getInboundEndpoint("uri").getConnector;
But there is no method to actually send the mulemessage.
Solved:
The problem is that you can not set inbound properties on the MuleMessage, and the flow is depending on some of those to function(path, query params etc).
It seems you are able to inbound scoped properties with this:
m.setProperty("test", (Object)"test", PropertyScope.INBOUND);
Is there a way to make this approach work, or an alternative way to access the flow? I tried using mulecontext to get the flow:
muleContext.getRegistry().lookupFlowConstruct("myflow");
But it did not contain anything that looked useful.
Solution:
As David Dossot suggested in a comment of his answer, I was able to solve this with muleClients request method.
muleContext.getClient().request(url, timeout);
Then constructing the url as usual with GET parameters etc.
I'm not 100% sure about what you're trying to achieve but anyway, the correct way of using Mule transports from Java code is to use the MuleClient, which you can access with muleContext.getClient().
For example, the send method allow you to pass a properties map that are automatically added to the inbound scope. Behind the scene, Mule takes care of creating the endpoint needed for the operation.
Regarding the flow: what are you trying to do with it? Invoke it?

Passing Variable from one website page to another website page without using QueryString

This question is related to a previous question
Passing Variable from page to page using ASP.NET (C#) without using QueryString
The difference in my case is that the request is coming from a different website (in java) to my website (in asp.net). I do not want the variable to appear in url.
Any suggestions !!
To explain my scenario, we are making a webpage(plugin), which can be called from any other website. To authenticate request, i am looking for a mechanism when other website will pass id & auth-key to my page. This i can use to authenticate the request. I do not want these variable to be visible.
A POST operation would work. The variable would still be part of the request, but it would not be readily visible to the user. I say "readily" visible because it won't be part of the requested URL, but it would be visible if they were to use a tool like Firebug. Short of sharing a database or some other form of "out-of-band" communication, I'm not sure it can be done any other way...
Well as chris mentioned doing a POST is the best way to achieve this. Else you can look at using javascript to achieve the same. Its pretty easy to use JS libraries to achieve the same.
Some of them that come to my mind are
a) Jquery
b) YUI
c) EXT (now Sencha i guess)
But I would definitely recommend jquery.
With jquery you have apis to do post operations. here is more on how to achieve the same.
http://api.jquery.com/jQuery.post/
Hope that helps.
I don't think it can be done without a query string. I know sessions won't work because sessions cannot be shared between Java, Asp, Asp.net, Php etc..., at least not nativly. If you have a database where you store the sessions, you can always use a session id in a query string and therefore simulate cross-language-sessions.

Java Web Application

I am interested in creating a simple web application that will take in user input, convert it to an XML file and send the file to a database.
Coding wise I feel I am okay, it is just the general setup and what implementation to use I am a bit unsure of.
At the moment I have a JSP page containing a form, the user fills out the form and on submit a POST method is sent to a servlet, in the servlet doPost() method the servlet is instantiating a java object and passing it the user inputted data. The java object then writes that data to an XML file and sends it to the database via REST.
All I would be interested to know is if this the standard/optimal way of creating such a web application.
Any and all feedback is appreciated.
Thanks
For a "simple webapplication" this high level approach looks fine in general. However, if you want more critical feedback, you'd need to give more details about the low-level approach. It may for example happen that it isn't memory efficient and thus may break when the webapp is been used by over 10 users concurrently, just to give an example.
I only question the choice for the GET method. You'd normally only use it to retrieve data (SELECT), not to create/alter data (INSERT/UPDATE/DELETE). For that you'd normally use POST, so that no one can execute it "accidently" by just clicking a (bookmarked) link. Changing GET to POST isn't that hard, add method="post" to the <form> element and rename doGet() to doPost().

Categories

Resources