WSO2: API Manager; calling publish api from webservice - wso2

Is it possible to call the WSO2 API Manager:: publisher api, as described in:
http://docs.wso2.org/display/AM150/Publisher+APIs
From a webservice, instead fronm JSON
Greetz,
Marc

You can simply call those jaggery endpoints from webservices.
Do a simple HTTP Post to those endpoints;
Eg:
// create a post request to addAPI.
String addAPIendpoint = "http://localhost:9763/publisher/site/blocks/item-add/ajax/add.jag";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(addAPIendpoint);
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(12);
params.add(new BasicNameValuePair(API_ACTION, API_ADD_ACTION));
params.add(new BasicNameValuePair(API_NAME, serviceName));
params.add(new BasicNameValuePair(API_CONTEXT, serviceName));
.....
HttpResponse response = httpclient.execute(httppost, httpContext);

Related

WSO2 Api manager- Respose body data is null using API SDK

I've created an API using WSO2 Api Manager and gave a backend application URL as a production URL. I'm getting 200 response code and Json responce body in web console of the API but when I use JAVA SDK of that API using client. I'm not able to get the response data even though I'm getting 200 response code.
The below mention java code I'm using to consume the API.
Thanks in Advance!
DefaultApi defaultApiTest = new DefaultApi();
ApiClient apiClient = defaultApi.getApiClient();
apiClient.addDefaultHeader("Accept", "application/json");
apiClient.addDefaultHeader("authorization", "Bearer " + "e88aa28a-4e0e-34a1-aec1-55616bf1e7a1");
apiClient.setBasePath("<Production URL of the API>");
defaultApiTest.setApiClient(apiClient);
ApiResponse<?> response = defaultApiTest.getWithHttpInfo();
if(null != response){
JSON data = apiClient.getJSON();
String value = data.toString();
System.out.println(value);
}

JavaCode To Connect API Mgr Using Client Id/Client Secret/Token

I have a BE Java Service, which is RESTFul, which is ported on WSO2 API Manager. It is published and available in Store. I have registered a new Application (by Name ' Java App ') and upon subscribing to that API, it provided me with Client Key and Client Secret along with Token. Using the Token I am able to successfully access the API (from SOAP UI). My requirement is to access the API from a standalone Java Application. Can someone direct me or provide appropriate code that can access the published API.
Regards, Sreedhar.
You can use Apache HTTP client to invoke the API by sending Authorization as a HTTP Header.
String url = "API_URL";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
// add Authorization Header header
request.addHeader("Authorization", "Bearer :" + accessToken);
HttpResponse response = client.execute(request);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
For Generating Token with username, password and Client Key / Secret, You can use following cURL sample to build the HTTP request. More information can be found in token api
curl -k -d "grant_type=password&username=<username>&password=<password>" -H "Authorization: Basic SVpzSWk2SERiQjVlOFZLZFpBblVpX2ZaM2Y4YTpHbTBiSjZvV1Y4ZkM1T1FMTGxDNmpzbEFDVzhh" -H "Content-Type: application/x-www-form-urlencoded" https://localhost:8243/token
You have to base 64 encode Client Key / Secret and send it with Authorization header as Basic.

UWP WebRequest Replacement HttpClient Cookie?

As i am either too dump to find the proper answer or it is simply not out there ... how the hek i replace the "outdated" WebRequest properly with the HttpClient "replacement"?
In the WebRequest i tendet to serialize & analyze the actual cookie as the webpage returns a partial JSON cookie ... however ... i still did not found a way to get a proper CookieContainer (or whatever form of cookie) from the frking HttpClient ... also ... every google request leads me to 20000000 years old answers or outdated documents (+ some upToDate docs which all just refer to "GET" requests without any cookies involved -.-*))
would be kindfull if somebody could lead me to the correct path ...
thx
greets
X39
Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient();
client.DefaultRequestHeaders.UserAgent.TryParseAdd(app.Settings.UserAgent);
var response = await client.PostAsync(new Uri(app.Settings.Pr0grammUrl.Api + "user/login"), new Windows.Web.Http.HttpStringContent(postDataBuilder.ToString()));
By default, HttpClient handles cookies by itself through the default HttpBaseProtocolFilter. You can get cookies associated with a URI through GetCookies method of the HttpCookieManager class:
Gets an HttpCookieCollection that contains the HttpCookie instances
that are associated with a specific URI.
using (var protocolFilter = new HttpBaseProtocolFilter()) {
var cookieManager = protocolFilter.CookieManager;
var cookies = cookieManager.GetCookies(uri);
foreach (var cookie in cookies) {
// Here is each cookie
}
}
You should also be able to set/get cookies through HTTP request and response headers. To disallow HttpClient from handling cookies by itself, create an instance of HttpBaseProtocolFilter and set the CookieUsageBehavior to HttpCookieUsageBehavior.NoCookies:
NoCookies: Do not handle cookies automatically.
// Create http filter
httpFilter = new HttpBaseProtocolFilter();
httpFilter.CookieUsageBehavior = HttpCookieUsageBehavior.NoCookies;
// Create http client
httpClient = new HttpClient(httpFilter);
// Handle cookies through HTTP headers

How to send http get request to servlet from restful webservice?

I am beginner in that, but
I have a restful web service and i want to send a http get request from it and handle the response in it. if any one knows how can i do this ?
i tried this :
#Context private HttpServletRequest servletRequest;
#Context private HttpServletContext servletContext;
but i want to know what's this injection will return to me? i don't understand how will get it and it's scope, and how to get the response?!
and how i will send the request?
i found this http client apache
and here is an example for sending an Get request and getting the response
http://www.mkyong.com/java/apache-httpclient-examples/
String url = "http://www.google.com/search?q=httpClient";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
// add request header
request.addHeader("User-Agent", USER_AGENT);
HttpResponse response = client.execute(request);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
i'll try and post any helpful edits i reach, anyone have another helpfull comments or edits please do.

communication between 2 rest web services

I have many rest controllers in my project (developed with spring MVC) and i would like to make them communicate between each other.
what's the best way to make two spring REST controllers exchange messages ?
Supposed you have 2 controllers:
#RestController
#RequestMapping("/first")
public class FirstController(){
// your code here
}
and
#RestController
#RequestMapping("/second")
public class SecondController(){
// supposed this is your FirstController url.
String url = "http://localhost:8080/yourapp/first";
// create request.
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
// execute your request.
HttpResponse response = client.execute(request);
// do whatever with the response.
}
For reference, have a look at this: http://www.mkyong.com/java/apache-httpclient-examples/
Used library: https://hc.apache.org/httpcomponents-client-ga/