wso2 API Manager and BAM - How to control API invocation? - esb

How can I retrieve the number of API invocations? I know the data has to be somewhere because wso2 BAM shows piecharts with similar data...
I would like to get that number in a mediation sequencel; is that possible? Might this might be achieved via a DB-lookup?

The way how API Usage Monitoring in WSO2 API Manager works is, there is an API handler (org.wso2.carbon.apimgt.usage.publisher.APIUsageHandler) that gets invoked for each request and response passing through the API gateway. In this handler all pertinent information with regard to API usage is published to the WSO2 BAM server. The WSO2 BAM server persists this data in Cassandra database that is shipped with it. Then there is a BAM Toolbox that has been packaged with required analytic scripts written using Apache Hive that can be installed on the BAM server. These scripts would summarize the data periodically and persist the summarized data to an sql database. So the graphs and charts shown in the API Publisher web application are created using the summarized data from the sql database.
Now, if what you require is extractable from these summarized sql tables then i suppose the process is very straight forward. You could use the DBLookup mediator for this. But if some dimension of the data which you need has been lost due to the summarizing, then you will have a little more work to do.
You have two options.
The easiest approach which involves no coding at all would be to write a custom Hive script that suits your requirement and summarize data to a sql table. Then, like before use a DBLookup mediator to read the data. You can look at the existing Hive scripts that are shipped with the product to get an idea of how it is written.
If you dont want BAM in the picture, you still can do it with minimal coding as follows. The implementation class which performs the publishing is org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageDataBridgeDataPublisher. This class implements the interface org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageDataPublisher. The interface has three instace methods as follows.
public void init()
public void publishEvent(RequestPublisherDTO requestPublisherDTO)
public void publishEvent(ResponsePublisherDTO responsePublisherDTO)
The init() method runs just once during server startup. Here is where you can add all your logic which is needed to bootstrap the class.
The publishEvent(RequestPublisherDTO) is where you publish request events and publishEvent(ResponsePublisherDTO) is where you publish response events. The DTO objects are encapsulated representations of the request and response data respectively.
What you will have to do is write a new implementation for this interface and configure it as the value for DataPublisherImpl property in api-manager.xml. To make things easier you can simply extends the exsiting org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageDataBridgeDataPublisher, write your necessary logic to persist usage data to an sql database within the init(), publishEvent(RequestPublisherDTO) and publishEvent(ResponsePublisherDTO) and at the end of each method just call its respective super class method.
E.g. the overriding init() will call super().init(). This way you are only adding the neccessary code for your requirement, and leaving the BAM stat collection requirement to the super class.

Related

call json web api nopcommerce

Hi I am new for nopcommerce 3.5. I need to write a restful web service api to third party(for eg mobile) access the service. I know that we can access through Nop.Plugin.Misc.WebServices . I had enable the service from administrator site.
But now is my question. How can i call the web service for eg GetPaymentMethod , product list and etc
And if I want to write my custom web service by using web api. what is step to create? I cant find any documentation about the web service. Please guide me some example
Thanks
If you want a really quick start in writing a web service in NopCommerce, you can follow the MVC architecture and:
Create an Action method inside a Controller that you find appropriate for your purpose. For example, if you want access to a product list, you might create an Action inside CatalogController that follows the logic of the existing ProductList action.
Set up a Route in RouteProvider.cs to point to the Action you created. Based on this route you can deduce the URL of your service.
Do the processing that you need inside the Action. If this Action/service is to be called with parameters (in query string format: param=value&param2=value2), you can just put these parameters in the Action's header:
public ActionResult QuickService(int param, string param2) { ... and .NET will take care of having them initialized.
Store results in an object (can also be an anonymous object) and at the end of your action, return it as Json: return Json(resultsObject); Again, ASP.NET takes care of the JSON serialization and automatically sets the Content-Type HTTP response header to "application/json".
You can consume the service calling the URL that corresponds to the route of your Action.
If you want users to be able to log in, by using the above method, it gets a little bit trickier. You need the webservice client to be able to accept and send cookies, as well as make appropriate services for Login, Logout, Register,...
However, in this case, you might be better off with a mobile version of the site.
Final note: If you don't want to alter base NopCommerce code, you can apply the steps above to a plugin. The majority of NopCommerce plugins follow the MVC architecture, so you can apply the steps above.

Why do I get a ValidationException

I try to extract the XML data for a form using the REST service like this :
http://localhost:8080/orbeon/fr/service/mysql/crud/<App-name>/<form-name>/data/<document_id>/data.xml
I first had to adapt the properties-local.xml to allow public access to the service, and now I can get in, but the server returns an HTTP 500, and I see a nice org.orbeon.oxf.common.ValidationException being throwed up.
In the stack trace I see that it tries to perform some XSLT transformation, but I only ask for the xml form data, so it should'nt. Or am I wrong ?
The MySQL Persistence Layer is working well.
Any hints ?
You can't directly call the specific persistence layers, because they need to receive special configuration headers. Instead, call the persistence service:
http://localhost:8080/orbeon/fr/service/persistence/crud/<App-name>/<form-name>/data/<document_id>/data.xml
The persistence service passes what is needed to the persistence layers.

Connect to MYSQL database, retrieve data as JSON and send it to client side via AJAX

I am building an interactive web application with GWT, and I've come across a problem. The app is basically going to be a GUI for a database.
What I'd like to do:
Populate a MySQL server with data, and serve it via AJAX as a JSON file to my client side code.
The application life cycle should look like this:
Query on the client side -> Query the database -> serve up the requested information -> convert it to JSON -> Send back to client side via AJAX -> process on client side
I'd like to make this without refreshing the page, so the database querying should be ajax too.
If someone could point me to the right direction, I'd be really grateful. I've yet to find any good tutorials or examples to this type of problem.
Using GWT:
1° For Data-oriented app you will want to use GWT RequestFactory
2° If you want to stick to basic RPC here's what happens:
Fill up a form > Click on a button > make a call using RPC, passing a "shared" object as argument to your call > conversion from JSON to Java is handled by GWT > handle the request and make your query > convert the entity/DTO to a GWT "shared" object > your RPC controller returns the result > conversion to Java to JSON is handled by GWT > typically use a Celltable to display the result using a dataprovider, you won't need to reload the page.
If some parts of the process are unclear feel free to ask.
Don't use JSON unless there is some other reason you don't mention. A strong point of GWT is that you can use your entity code in your client side code so all of the client-server communication layer is hidden. Easiest way to do what you are asking:
Create #Entity annotated objects for each table
Create RPC service that exposes operations client needs
Implement database interactions with Objectify
Fetch your entities in GWT using RPC client

How to preserve data through AngularJS routing?

I'm new to AngularJS and am trying to build myself a simple little app. I have JSON data for the app being fetched with $resource, and this data should be the same across multiple views/routes. However, when I go to a new route, the JSON data (stored as $scope.data) is no longer available to the new view. What can I do to pass this data to the new view and not require another fetch? (The tutorial phone-catalog app re-fetches this data every time from what I can tell.)
From what I understand, $rootScope can do this but it seems to be generally frowned upon. I apologize if this doesn't make much sense; I'm very much diving in the deep end here.
Use a service to store the data. Inject that service into each controller that needs access to this data. Each time a controller is created and executes (because you switch to another view/route), it can ask the service for the data. If the service doesn't yet have the data, it can make a request to the server and return a promise to the controller (see below for how to do that). If the service has the data, it can return it to the controller immediately.
See also Processing $http response in service
Note that services are singletons, unlike controllers.
Another variation: when the service is created, it can go fetch the data itself, and then store it for later use. Controllers can than $watch properties or functions on the service. For an example of this approach see How do I store a current user context in Angular?

grails to build Web interface using REST API

I am fairly new to Grails, and I have a few questions on how to proceed.
I have a REST API which I will use to retrieve the data , for exemple :
http://localhost/api/data/list
Which gives this result :
{"data":[{"col1":"blabla","col2":0},{"col1":"moreblabla","col2":1}]}
I want to use Grails to build an interface for those data (show, edit, add, delete)
Should I create a domain called Data ?
How do I tell Grails to to use the REST API and not a database ?
I am really clueless so I hope you can light the way ;)
Thank you.
Grails currently doesn't have a GORM plugin for using a REST endpoint as a persistence store. That is planned functionality, but is not slated to land until later this year (2012 - Q4).
That being said, you can write a service that will allow you to do basic CRUD operations on an object and get/persist to and from your REST endpoint. The place to start with that is the HttpBuilder, and perhaps the REST client plugin.