How to construct client context in AEM by passing values from an external system? - aem

Is there a way to construct the client context in AEM by using values passed by an external website? The external website sends the user information such as IP address, page data, geolocation, etc. I want to construct the ClientContext JSON without using CQ_Analytics.ClientContextMgr.init, as the information is from an external system.
For eg: We know the location of a visitor to our website. We want to pass this value to AEM and set the client context so that we can get the targeted content for this location.
Our end goal is to get something like this (but we are trying to achieve this without constructing the JSON by ourselves):
CQ_Analytics.ClientContextMgr.clientcontext = JSON.parse('{"profile":{"country":"US"}}');
Is there any Javascript APIs provided by AEM to construct the JSON?

You need to extend the client context using AEM documentation provided at:
Creating a Custom Context Store Component
Follow the instructions till the Initialization part where you will need to populate the data in the jsp file for your extension.
So in your case it would be something this:
if(!locstore){
locstore = CQ_Analytics.JSONPStore.registerNewInstance("<%= store %>",
"<%= jsonpurl %>",{});
}
Where jsonpurl will be the location of your service that provides the external data in json(p) format. This will initialise your store with the values you want and you won't need to worry about the manual json handcrafting.
Client context is constructed on client side using the JS library in AEM. You will need some binding parameters to connect your external data source to the current client context.
The detailed tutorial can be found here.

Related

How to get an asset all details in AEM with API request?

I am using AEM API in my automation. We published a file from workfront to AEM. That file have multiple information:
1. Basic
2. Test1
3. test2
4. etc
When I hit the api:
/api/assets/..../abc.pdf.json
I am getting very less information under the properties and metadata section. (In short, it does not have Test1, Test2 and other tab information)
Is there any way to fetch all these from aem API's? It will reduce my overhead to validate these details from UI Automation.
First of look checkout the metadata nodes of file for the desired data to be present. I would rather prefer writing a custom servlet in AEM which takes in path parameter as an asset path and gives back the response back in desired format.
I figured it out.
The following API returns all the JCR content in API response:
<baseURL>/content/dam/path to that file.-1.json

Rest API Hateoas : Should API response have identifiers as hard coded or as placeholders?

Link to the HATEOAS This is the link to the Hateoas article (snapshot below) where the identifiers of the resource is part of the URL i.e. 12345. Here the API response has the final API relative URL i.e. /accounts/12345/deposit and the client just needs to hit it.
Link to the Github Users API This is the link to the Github API (snapshot below) where there are lots of placeholders for identifiers. How will clients modify these URLSs and add a value in these placeholders? For example, {/gist_id}, {/other_user}.
Isn't passing the URL with id value instead of placeholder better? Why and when to rely on different clients to add values in these placeholders?
Hypertext as the engine of application state (HATEOAS) is a bit more than just the usage of links. In essence it enforces the interaction model that is used on the Web for two decades quite successfully. On the web a server usually "teaches" clients (browsers) to achieve something via the help of link relations, that can be used to automatically download related resources or give a hint on the reference resource, and Web forms, that define the syntax and semantics of each of the respective supported (input) elements, i.e. a text field, an option element to select one or multiple choices, a drop down or even a slider widget. Based on the affordance of each of the elements a client knows i.e. that a button wants to be clicked or pressed while a text fields wants some user input and stuff or a link annotated with the prefetch link relation name may be downloaded automatically once the current page finished loading as a client might invoke it next or a preload link relation might instruct a user agent to load the referenced resource early in the current page loading process.
The form not only teaches a client about the supported fields a resource has but also about the target URI to send the request to, the HTTP method to use wile sending the request as well as the media-type, which in the case of Web forms is usually implicitly set to application/x-www-form-urlencoded.
In an ideal world a client just uses the information given by the server. Unfortunately, the world isn't perfect and over time people have come up with plenty of other solutions. Among one of them is URI templating that basically allows clients to use a basic URI and fill out certain placeholders with concrete values. As making use of templating requires some knowledge of the URIs intention or the parameters you need to pass, such capabilities make only sense as part of media-type support.
Plain JSON (application/json) has by default no support for URIs whatsoever and as such a user agent receiving a plain JSON payload might not be able to automatically replace a template URI with a concrete one out of the box. JSON Hyper-Schema (application/schema+json) attempts to add link and URI template support to plain JSON payloads. A user client though needs to be hinted with the appropriate media-type in order to automatically resolve the full URI. As such, the user agent also has to support that respective media type otherwise it won't be able to process the document (resolve the template URI to a real URI) successfully.
JSON Hypertext Application Language a.k.a HAL JSON also supports URI templates for links. application/collection+json does support two kinds of templates - query templates and objects-template. The primer one is similar to a URI template by allowing to append certain query parameters to the target URI upon sending the request while the latter one allows to define a whole object that contains all the input elements used to add or edit an item within the collection. JSON-LD does not really support URI templating AFAIK though it uses the concept of a so called context where certain terms can be used to abbreviate URIs. As such something like name can be used within the context for a URI like http://schema.org/name.
As you can hopefully see, the support for URI templating depends on the media-type used for exchanging data. In the case of the outlined github example GET /users/:username this more or less resembles a typical Web API documentation, similar as it is done in a Swagger API documentation, that unfortunately has hardly anything to do with HATEOAS.
For your top example (banking), you should absolutely include the complete URL, with account numbers (IDs), so that the client does not need to translate/substitute anything. This is the most common scenario with HATEOAS. However, GitHub does have those "placeholders" for endpoints that could contain multiple values. You can't include the "following_url" for every user in the response, it's not practical. So you have to determine the "other_user" value another way and make the substitution. Personally, I haven't even had this use case with any of my applications and all of my HATEOAS URLs resemble you first example (though I prefer full URLs not relative). Unless you have specific cases like GitHub does, it's not necessary to use any of these placeholders. Even GitHub only uses that where they could be multiple values. For fixed value URLs, they have the username (like your account number) in the URL ("octocat").
According to me we should not give the direct url in the body
We should always parameterized the api and get details form there.
In simple case if Id of data change than every time data need to update for detail url.
Else if it’s dynamic you will never face this issue.
And this also come under best practices.

dotCMS REST API fetch template by its id

I consider using dotCMS and integrate it with other system by its rest API but in spite of ability to fetch content by its api I cannot fetch tempates defined in dotCMS.
With url api/content/type/xml/id/c12fe7e6-d338-49d5-973b-2d974d57015b I obtain as response following xml:
<contentlets>
<content>
<template>1763fa6e-91c0-464e-8b16-9a25d7ae6ce5</template>
<modDate>2015-12-10 10:58:56.098</modDate>
<cachettl>15</cachettl>
<title>About Us</title>
<httpsreq/>
<showOnMenu>true</showOnMenu>
<inode>84e2879a-7749-40f4-bded-9d59dbb2b1da</inode>
<____DOTNAME____>About Us</____DOTNAME____>
<disabledWYSIWYG>[]</disabledWYSIWYG>
<seokeywords>dotCMS Content Management System</seokeywords>
<host>48190c8c-42c4-46af-8d1a-0cd5db894797</host>
<lastReview>2015-12-10 10:58:56.093</lastReview>
<stInode>c541abb1-69b3-4bc5-8430-5e09e5239cc8</stInode>
<owner>dotcms.org.1</owner>
<friendlyname>About Us</friendlyname>
<identifier>c12fe7e6-d338-49d5-973b-2d974d57015b</identifier>
<redirecturl/>
<canonicalUrl/>
<pagemetadata>dotCMS</pagemetadata>
<languageId>1</languageId>
<seodescription>
dotCMS Content Management System demo site - About Quest
</seodescription>
<folder>1049e7fe-1553-4731-bdf9-ba069f1dc08b</folder>
<sortOrder>0</sortOrder>
<modUser>dotcms.org.1</modUser>
</content>
</contentlets>
Is there any possibility to fetch template by its id (here 1763fa6e-91c0-464e-8b16-9a25d7ae6ce5) and obtain html file as response or some xml with html content?
Ok so after some hours of source code analysis I think I can assume that there is no rest api implemented by dotCMS. Instead of this we can use java api and fetch such components like templates, containers etc. by calling methods which are responsible for fetching these components from database. Then we can implement our own rest services.
Templates in dotCMS govern the display of "pages" which can be made up of multiple content objects. If you want to return a content object that has formatting applied to it, take a look at the widget API here:
http://dotcms.com/docs/latest/remote-widgets
and how it can be used here:
http://dotcms.com/docs/latest/remote-widgets

Jira calls external REST Service

My Problem: I want to introduce a new field in JIRA with status information from external REST Service (response is json).
Plan: Every Jira issue has a input field with some reference string. Behind this field there should be a panel, what should display informations from the external REST call (parsing response JSON is required).
Can someone give me some good info pages, how to tell JIRA to call external REST Service?
If you don't want to build it see:
nFeed
HTTP Feed Custom Field
If you want to build it yourself then start by following this tutorial on Creating a custom field type which is to more or less store a basic String within the database. (This would be the reference string)
You then have two options, the first is within the JiraCustomField class override the getVelocityParameters which was taken from How to call a java method from velocity Atlassian Answers question.
Then create a method (fetchValueFromWebService(String val)) that you would call that would contain code to query the REST Service based off the fields value that would be passed in from the velocity template. (E.g. $instance.fetchValueFromWebService($value))
To perform the actual web service call you can use any library you want, just see the Managing Dependencies documentation so it gets included in the plugin. (For example using the Jira Jersey version see this)
Your other option would be to within the view-basictext.vm have it use javascript and perform an AJAX to the web service by calling a function in your own JS file and dump that into a span that you have defined: (See Including Javascript and CSS resources)
<span id="webServiceValue"></span>
<script type="text/javascript">
fetchValueFromWebService($value);
</script>
You would however need to ensure that the webservice has Cross-origin resource sharing (CORS) enabled if you go the AJAX route.

Why GWT URL doesn't change on an event or a service call?

I have two questions:
Q: 1
I'm currently developing a GWT app. The entry point for the app is: ImageViewer.java. I could well access it by http://127.0.0.1:8888/ImageViewer.html?gwt.codesvr=127.0.0.1:9997. I have a service called "Search" which has corresponding "Async" and "Impl"'s defined. Now, I call the service from client side, using RPC. I could call the service, obtain return value. Everything works fine.
However, I expect the application to show a behavioral change on URL. i.e. when a service is being accessed, I thought it would be reflected on the browser's URL something like: http://127.0.0.1:8888/search?gwt.codesvr=127.0.0.1:9997 as I've modified web.xml. However, this behavior is not realized. Any particular reason why this is not reflected??
Q:2
This one is a reverse of the previous ques. i.e. I have an application running. Let's say it has an entrypoint class(Imageviewer.java) and another composite class (searchClass.java) which would be loaded on the Imageviewer based on an event. This searchClass invokes the "search" service mentioned in the previous question.
I could load the "searchClass" in "Imageviewer", invoke the service, and the service also returns the value needed. Everything works fine... But,
I need something like this: by just typing this query string:
http://127.0.0.1:8888/search?value=John
I want the "searchClass" to be loaded on the "ImageViewer", call the service using the value(which is "john" in this case) and display the result. Is this possible at all?
what I've tried: I have tried to create a httpServletClass on the server and mapped it with the URL and could do the search. The search returns appropriate results. However, I want the results from the server to be displayed on the client. Remember, I'm directly using a servlet to read the URL and so there is no value being passed from client to server.
Thanks in advance.
A: 1. To change URL, the hash part, you need to set new history token in the History class. More about history management in this article.
A: 2. For the second part you could achieve it by changing the history token, for instance "http://127.0.0.1/search#value=John". The history service will trigger an event if the # part changes. You could also use the part with "?", as in your example, if you use Window.Location , but it will cause reload of the application, which would put the whole idea of using GWT in question.
RPC (AJAX) calls are done Via XHR and do not change the browser URL.
You can't (with the URL you presented). GWT apps normally run in one web page, i.e. the URL does not change (see how gmail changes browser url bar). What you can do is enable GWT history support. Then your url would be http://host/#search?value=queryu