How can I wrap the Sitecore 8 itemWebApi to apply custom business logic to API responses? - sitecore

I have a requirement to build a service endpoint to provide specific Sitecore 8.0 items (containing a given field value in a given branch of the content tree) to requesting mobile app clients. Encapsulating this logic (and perhaps some other calculations, etc) means the out-of-the-box API is not suitable.
I'd like to mimic an existing SOAP service exposed by another CMS, however I'm not above using a modified version of the RESTful itemWebApi if it confers greater code reusability or upgrade-safety.
Based on my research thusfar, it would appear my options are to build a custom handler, a completely separate asmx service (ala this approach), or to build a custom controller (similar to this custom Web API controller method).
Overriding or replacing the default pipeline processors for the itemWebApi does not seem viable, as I don't want to replace/modify the OOB API if I can avoid it.
Has anyone with the same type of requirement for Sitecore 8 found a better approach?

The approach I chose was to create a separate service "router" developed using the adapter pattern to be consumed by our mobile app clients. The router in turn calls the Sitecore ItemWebApi.
This fit my needs the best as it is completely decoupled from the Sitecore application and the client can be modified if necessary without impacting the endpoint.

It would also be worth looking to EntityService within Sitecore.Services.Client in Sitecore 8. Its a Web API based service but has more flexibility over the standard Sitecore Item Web API because you can define your model and the business logic yourself.
I can see you have mention my other blog post on adding a custom Web API controller. Enitity Service is different, it's a framework by Sitecore to achieve a standard way of creating custom web service for Sitecore.
I have written a blog post on EntityService. It has both a Javascript and standard rest based API to communicate with the service too.
http://mikerobbins.co.uk/2015/01/06/entityservice-sitecore-service-client/
Example Application here: https://github.com/sobek1985/EntityServiceDemo
And a few more posts on advanced features in Entity Service: http://mikerobbins.co.uk/category/sitecore/sitecore-service-client/

Related

API Goverment rules in WSO2

I would like to implement automatic rules of API Goverment in a WSO2 API Manager platform like, for example, validating context with a regular expresion, or version numbering or API name or API resources endpoint naming or parameters, etc.
I checked in version 2.6.0 (and previous versions of major 2) that it can be done in jaggery apps of publisher, but this is a mix of data and presentation (view structure with js and html) and it is not a clear and right way to implement it.
Is there any rules engine or other dedicated mechanism to do it? If not, is it in the roadmap of WSO2 AM to add this kind of features? It would be great.
APIM 3.x onwards UIs are implemented using react and backend services are exposed via REST APIs. SO from this version onwards, this is clearly separated. UI level validations can be changed in the React and if there are any additional validations required, REST API interceptors or workflow interceptors can be used to enforce any validations.

Sitecore Services Client vs Sitecore Web Service vs Sitecore Item web API

I'm a Sitecore beginner, working on Sitecore 8 with ASP.NET.
There is a page where an Ajax call has to be made to get data.
Created a test web service in the project which responds to the ajax call with success. This service will be a wrapper.
I understand, I have to consume one of the 3 options (in the title) inside the test service, to get the Sitecore items and do stuff.
But all 3 appear similar to me & not sure which one to go with, to achieve my task
Also,
1. Is there an advantage of one over the other.
2. ..and when to use which one.
Sitecore Services Client is the framework or namespace that both Item Service and Entity Service sit within. Item Web API is the legacy framework.
Use Item service if you want to do basic stuff with the sitecore Items as this is a prebuilt api. Entity Service allows you to create your own API using the Sitecore.Services.Client framework and give you much more power and control.
https://mikerobbins.co.uk/2015/01/06/entityservice-sitecore-service-client/
Use can also use Sitecore.Services.Client to do the routing and you write the controller. see https://mikerobbins.co.uk/2015/06/25/sitecore-service-client-servicesapicontroller/

Login/Registration functionality in cq5

I need to create register/login functionality in CQ5. There are three ways
1 - The register/login functionality should be prepared as web service hosted on a Java Application Server and then we can consume data from web services.
2 - The functionality should be prepared as web service based on RESTFull that hosted on CQ itself since AEM is developed around the REST principal and REST web services is certainly one which will be supported. So I believe RESTfull web services can be hosted with CQ5.
3 - Why do we need web services? I do not think it requires web services since my application is not going to communicate with other application and it dont want to expose any services to outside world. Why don't we create register/login functionality as we do in normal web application. Can we create a OSGI bundle that invoke databases to store user details for register/login functionality?
I would go for 3rd option but my concern is AEM is not the place to build our business logic to carry out functions that are not related to content rendering.
which option would you choose and why? Kindly share your ideas and thoughts. I am just looking for best solution to develop register/login functionality in CQ.
Thanks
I think i am late to the party but it might help someone. Yes, you can very well create rest webservices in CQ and they work well. So, you can take that approach.
The way to go about implementing login/registration would be how it is done OOTB but OOTB is also bad in the way it implements it so you have to make modifications to overcome that.
1) You need to register and Authentication handler with CQ. If you are looking for a sample there is a form authentication handler source that you can look at.
2) We have integrated out Auth handler with Active Directory (AD). Since AD is supported by default, so it is easy to integrate with Auth Handlers.
3) Next you need to create a user login token for which you can look into TokenUtil class of CQ or if you are looking to develop SSO, you can look into SSO code that is packaged OOTB.

Difference between portlet and a webservice

I am not sure if I am comparing onion with a cabbage. But I am really confused with the portlet and a web service. I know that web service is making your software components in your application as a service to other applications but to me it is similar to a portlet responding to requests. I know that my understanding is making you(Portlet & SAAS developers) really upset. But I need some clarity on this.
Why cannot I use the request-response to portlet work similar to a web service?
If yes, what is the benefit I get in using a web service than a portlet?
If not, then what are portlets lacking to act as a web service?
I am a newbie in portlet development. Please dont curse me for this question.
Please suggest.
EDIT:
Most of the answer say that the portlet is a UI component. My customer has requested me to create a webservice in liferay. So is it to make a portlet as a web service? If yes, can you please provide a pointer or some code snippet?
Addressing your 'edit' specifically with regards to Liferay...
Liferay plugin projects can expose web services without too much work.
The Liferay Service Builder actually has facilities so you can create SOAP web services or JSON web services backed by tables in the Liferay database (and that make queries on existing database tables). In the case of JSON web services, the URL scheme generated is not RESTful (unfortunately), but it is well defined. With a bit of work on your part you can impose Liferay's permission system on web service calls as well.
Here's a reasonable starting point in the Liferay Developer's Guide:
See the links on that page to SOAP and JSON services.
Note that a 'portlet' is only one type of Liferay plugin. The web service facility is part of the service layer - which can be used by many plugin types. So there isn't necessarily a portlet involved in creating a web service in Liferay, you can make a web service that has no visual component associated with it.
Portlets are areas you create to add and display content. Portlets can be added to the left and right of the center body area and can contain myriad content ranging from news, events, calendars, RSS feeds, collections, plain text, and more. Also, you can set properties and policies for showing the portlets.
you want to use portlet when
You have content you want to have special focus, like news.
You need to specify the configuration data for an item. - i.e. number of entries to show.
You want to give the content editors a choice about when and where to display content.
You'd like to show it only to some groups or users.
On the other hand, web services:
Enables applications to expose their services
“progammatically”, i.e. the services can be invoked by programs
Enables software running on other computers (could be a desktop, mobile phone, PDA, etc.) to invoke operations exposed by Web applications
if you are trying to identify the main difference portal historically, the portlets have three layers architechture (client, middle tier, backend) this cause two weakness:
Different portal groups have no well established way of sharing services
Different technologies(Java, Perl, CORBA, EJB) in middle tier.
Different protocols(GRAM, IIOP,...)
Consequently, lots of redundancy, reinvention
Example: batch script generation
so Web services address the service sharing problem and reduces the redundancy.
source1 source2 source3
Portlets are meant to be a user interface component in a portal solution.
Web Services provide functionality to remotely interact with a system commonly using SOAP, REST, JMS or other related
They provide completely separate functions. The closest you get is the serveResource method in portlets. This functionality is used to serve various types of content from a portlet (such as a PDF document or an AJAX response) without the need to perform a full page request.

Consuming a ASP.NET MVC 2 JsonResult like a web service in a web forms C# application?

A friend wants to consume my ASP.NET MVC 2 application in a similar fashion as adding a web reference to it, accessing my functions, and using my model objects from a .Net web form from a separate website.
Any links out there that could explain how to "dress" my MVC responses so that his server to server consumption would be similar in experience to a web service?
I suggested using System.Net.WebClient to pull the results in to a variable then deserialize the JSON result, but maybe there's a better approach out there?
I'd suggest that you consider an API controller or a separate API application depending on the load you expect from people consuming data from your application. A separate API application will allow you to move it off your application servers if needed.
Rarely, will you find that the data that you would provide via an API is a one-to-one match with what your views need to be rendered. Behind the scenes you could abstract the data generation so that your API and your application controllers reuse the same code to get at the data, but the front-end of the API would understand how to negotiate security (from an API perspective) and present data that is easily consumed by a program. Moreover, you won't find that you're creating extra controllers and methods in your application just to provide some data that will never be used in a particular view.
You could use MVC or WCF for the API and JSON or XML as the payload format. If you use WCF, you get the benefit that he really can simply add a service reference to connect to it without you having to build a WSDL file/action.
From another's advice, Phil Haack added this to MVC 2 Futures. Add the DLL reference to the Application Start, and bingo. It uses a validator.