AKAMAI CCU Fast Purge implementation - java

Am trying to implement the CCU fast purge call via JAVA and am referencing this doucument
https://developer.akamai.com/api/purge/ccu/reference.html
import javax.ws.rs.core.MediaType;
Client client = ClientBuilder.newClient();
Entity payload = Entity.json("{ 'hostname': 'www.example.com', 'objects': [ '/graphics/picture.gif', '/documents/brochure.pdf' ]}");
Response response = client.target("https://private-anon-3f6068ab95-akamaiopen2ccuccuproduction.apiary-mock.com/ccu/v3/delete/url/{network}")
.request(MediaType.APPLICATION_JSON_TYPE)
.post(payload);
System.out.println("status: " + response.getStatus());
System.out.println("headers: " + response.getHeaders());
System.out.println("body:" + response.readEntity(String.class));
here am getting confused with the objects and the client.target url that needs to be specified.
" objects': [ '/graphics/picture.gif', '/documents/brochure.pdf'
] "
Are these objects and client.target urls will be unique to my application and the account that is going to be created.?
and also is there any thing that i have to pass in headers for the validation or authentications.?

You need to:
Get credentials for your client using Luna (https://control.akamai.com)
Click "Configure... Manage APIs"
Select "CCU APIs" in the left hand side
Create new collection, create new client
Create authorization
Click the "Export" button at the top to grab the credentials, including the URL
Use these credentials and the Java signing client at https://github.com/akamai-open/AkamaiOPEN-edgegrid-java to make the call
There is sample code on the page for the Java signing client showing how to make calls to the system.

Related

Create digest auth header

I want to create a header in order to get a resource from a web service which is protected by a digest auth, I have the login and password but I did not find the right way to create this header like postman.
There're two key points, one is calculate the auth key, the other is pass key in request header. Depending on the service authentication method, you can read documentation of the service to do the calculation. I focus on later part, that you can pass the key in header like below:
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.StringEntity;
public void post() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = "http://127.0.0.1:5050/vaccine/report";
HttpPost post = new HttpPost(url);
post.addHeader("x-auth-token", "computed value");
String body = "{\n" +
" \"siteId\":3,\n" +
" \"injectCount\":3,\n" +
" \"email\":\"yanghe#xyz.com\"\n" +
"}";
HttpEntity entity = new StringEntity(body, ContentType.APPLICATION_JSON);
post.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(post);
System.out.println("response code: "+ response.getCode());
}
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1.3</version>
</dependency>
No example like the source example. If you want to use request headers:
Select the API Key option from the list in the request Authorization tab
Enter your key in the dialogue box in front of “Key.”
Enter the value corresponding to your key in the dialogue box in front of “value.”
Select between Header and Query params from the list.
This is what the Postman team are saying:
"This method of authorization is a little more complex than the
methods mentioned above. In this method, the server sends some details
when a client sends the first request to the API. During this time,
the server generates an encrypted string using this passed data and
stores it for future purposes. This data contains a “nonce” field ( a
number that you can use only one time), other few details, and a 401
unauthorized response.
After receiving these details from the server, send an array of
data(encrypted), which also contains username and password and the
data that the server sent you in the first place.
When you send the second request, the server matches the stored
encrypted string with the data you sent in your request and decides
whether to authenticate you. Select Digest Auth from the dropdown
list in the Authorization section of a request to use this method."
And they provide exactly how to enter a digest request on Postman:

What is a client key and how to generate one?

I'm trying to send an https request to a server using Java. The URL to which I'm connecting needs the clientkey.
The URL is: "https://www.zipcodeapi.com/rest/"+clientKey+"/info.json/" + zipcode + "/radians";
How would I get the client key?
I think so you need to register on this website to generate a client key , basically these keys are like an access token to the web services you want to consume.
You can register but as the website says "free account allows up to 50 API requests per hour. Complete this form to get an API key to start using the API"
https://www.zipcodeapi.com/Register

Login authentication using angular + java rest api

I have been trying hard to work this out for login authentication using angular as client and jersey exposed as rest web service at backend.
Here is what I achieved from last three days.
Angular code to capture email and password:
myApp.controller('loginController',['$scope','$http', function($scope, $http)
{
$scope.email = "" ;
$scope.password = "" ;
$scope.loginForm = function(){
alert("login controller called");
console.log($scope.email);
console.log($scope.password);
var encodedString = 'email=' +
encodeURIComponent($scope.email) +
'&password=' +
encodeURIComponent($scope.password);
$http({
method:'POST',
url: 'rs/loginResource',
data: encodedString,
headers: {'Content-Type' : 'application/x-www-form-urlencoded'}
});
};
}]);
Java rest code:
#Path("/loginResource")
public class LoginResource {
public LoginResource() {
}
#POST
#Consumes("application/x-www-form-urlencoded")
public void login(#FormParam("email") String email,
#FormParam("password") String password) {
System.out.println("Email is: " + email); //prints output
System.out.println("Password is: " + password); //prints output
}
}
And now my question is where to go from here after getting the POST data from form submit. As you can see I am just printing the values rather I would like to check the email and password against database(oracle). How would I go about it? Shall I use simple connection class and dao or go for JPA which I haven't learned yet - what is the learning curve for it?
Is there any design pattern involved? Usually I use Dao and pojo if its plain java but I am new to rest api that too struggling with angular. I hardly find any examples on vanilla java+angular where most of them are based on spring+angular.
Generally login goes like this:
Client calls server with login details
Server verifies login details against the database, if valid, sets up a session. If invalid, the server will return a very generic error response. Important to not give the client any info about which part of the submission was wrong (gives attackers more info).
For this you'll want to read into sessions. Here are some links:
https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpSession.html
https://medium.com/opinionated-angularjs/techniques-for-authentication-in-angularjs-applications-7bbf0346acec#.z4vdsyrty
There's plenty of information on this problem on the internet.
Also, for generic REST APIs authentication will usually happen in the form of a token. The flow looks a little different:
Client calls server with some sort of auth info
The server generates a token using something like Json Web Tokens and returns it to the client. Generally these have an expiry. The server might also expire all other tokens for the user.
The client sends the token, generally as a header, with every future request.
There's lots of ways to encrypt a password when sending it from client -> server. Here's a simple one I suggest you try: RESTful Authentication

jBPM 6 API usage to call REST API

I looked through the jBPM6 user guide, and I found that there are URL's available for each operation, like: http://serverurl:8080/business-central/rest/task/query?potentialOwner=bpmuser
This URL is used to get the tasks assigned to bpmuser. I'm able to request this URL using Google's REST client without any errors. The problem is though, that I'm getting an authorization error when I try to request this URL from my Java program. Could anybody help me with the problem I'm having?
You can send authentication details in header as below.
String authData = "krisv" + ":" + "krisv";
String encoded = new sun.misc.BASE64Encoder().encode(authData.getBytes());
get.setHeader("Authorization", "Basic " + encoded);
See my answer in this thread for a working example.
Did you read the documentation?
http://docs.jboss.org/jbpm/v6.0.1/userguide/jBPMRemoteAPI.html
Are you sending the user / password?
Look at this section -> 17.1.1. The REST Remote Java RuntimeEngine Factory

Where does Android RESTful POST go?

I want to first say that I am a newbie to Android and java (to a lesser extent). I have a client-server application, the client is an Android app and the server is running Tomcat. Considering I am new at this I am a little confused about the POST request. The Client has a couple text fields, the user enters information and hits a button that calls on a method that well does a POST task. I can see that the server receives the data from the POST form in the client but my question is where does that information (from the form) go? It is supposed to create a new resource (in this case a Person resource)...Here is the code from the PersonResource class that does the POST.
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
#Produces(MediaType.APPLICATION_JSON)
public Person postPerson(MultivaluedMap<String, String> personParams) {
String firstName = personParams.getFirst(FIRST_NAME);
String lastName = personParams.getFirst(LAST_NAME);
String email = personParams.getFirst(EMAIL);
System.out.println ("System storing: " + firstName + " " + lastName + " " + email);
person.setFirstName(firstName);
person.setLastName(lastName);
person.setEmail(email);
System.out.println ("person info: " + person.getFirstName() + " " + person.getLastName() + " " + person.getEmail() + " " + person.getId());
return person;
}
It returns a person resource but ultimately where does that person resource go? I am sorry if I have not provided all required information needed to solve this problem. If more information is needed I will be happy to provide it. I truly appreciate any help given. Thank you.
I am not sure I fully understand the question, but here is an overview that might be helpful:
The client POST sends an HTTP request to the server. The server must have some sort of web service framework (e.g. Jersey or CXF or ...) that processes the request. The JAX-RS annotations on your class (#POST and #Consume), instruct the web service framework to route the request to the postPerson method of your class. It sounds like this much is working, yes?
Your method then constructs a Person object based upon the contents of the form, i.e. the user input. Your method returns this Person to the web service framework.
So what happens to this person? The #Produces annotation you have provided, instructs the web service framework to generate a JSON representation of the person and include this in the body of the HTTP response that is sent back to the client. The response might look something like this:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 259
{"fistName":"Bob", "lastName":"Barker", "email":"bob_bark#priceisright.com"}
Is this what you were looking for?
You can use HttpPost to submit the request and then use gson to convert the response back to object. The HttpPost should be placed under async request (i.e AsyncTask). You can refer
http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
Async HTTP post android
http://code.google.com/p/google-gson/

Categories