Hi I am getting the access_token value dynamically from server for servlet request it is below: now i want to retrive the access_taken value in to my servlet program.
{
"access_token":"AQWP_EVkqdc7E0wD09J6msmjnUlvilhP304gUIDzl6KKgNxwnHyz_W9nOxS0IroDJwEfVr3n3O-IC9YKc3bjyuwYRm6qbKRiP3A2AzuDjo8ohZERZFRCMyfjjqqDjNJ5J5ReCQDhkFJam51eiqsOeXDg4U_c9XJzc1dUx7Qxck0p9RNE0",
"expires_in": 5183999
}
my code is below:
public class Demo extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~:(first-name,last-name,email-address)";
/**
* #see HttpServlet#HttpServlet()
*/
public Demo() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out=response.getWriter();
String authCode = request.getParameter("code");
request.setAttribute("authCode",authCode);
out.println(authCode);
doPost(request,response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out=response.getWriter();
Object url=request.getAttribute("authCode");
response.sendRedirect("https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code="+url+"&redirect_uri=http://localhost:8080/LinkedinMails/dem&client_id=xxxxxxxxxxxxx&client_secret=xxxxxxxxxxxxxxx");
}
}
Thank You.
public String doGet(String url) throws Exception {
URL urlObj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(true);
String line;
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
conn.disconnect();
return buffer.toString();
}
Now you can call this method from your servlet to get server response in servlet. Hope it will useful to you.
You can use Gson or Jackson libraries for conveting your JSON response.
Using Jackson:
new ObjectMapper().readValue(result, LinkedIn.class);
where result is your JSON that contains access_Token and expires_in
LinkedIn.java
#JsonIgnoreProperties(ignoreUnknown=true)
public class LinkedIn
{
#JsonProperty("access_token")
private String accessToken;
#JsonProperty("expires_in")
private long expiresIn;
// Getters and setters for expiresOn and accessToken
}
In your Servlet/Controller
LinkedIn l= new ObjectMapper().readValue(result, LinkedIn.class); // Result is your JSON response which has access token and expires from LinkedIn
l.getAccessToken() ; // Returns you access token
l.getExpiresIn() // Returns you the expires time frame
}
Using JSONObject
String s="{\n" +
" \"access_token\":\"AQWP_EVkqdc7E0wD09J6msmjnUlvilhP304gUIDzl6KKgNxwnHyz_W9nOxS0IroDJwEfVr3n3O-IC9YKc3bjyuwYRm6qbKRiP3A2AzuDjo8ohZERZFRCMyfjjqqDjNJ5J5ReCQDhkFJam51eiqsOeXDg4U_c9XJzc1dUx7Qxck0p9RNE0\",\n" +
" \"expires_in\": 5183999\n" +
"}"; // This contains your access token JSON
JSONParser parser = new JSONParser();
JSONObject o = (JSONObject) parser.parse(s);
System.out.println("Access Token: "+o.get("access_token")); //returns your access token
System.out.println("Expires: "+o.get("expires_in"));
Getting the Access Token JSON:
In your Demo Servlet, doPost method, instead of sendRedirect do a
http Post (Using Apache Http Client or java.net)call to the url.
Once the HTTP Post is done, You get back a Http response which will be application/json content. So get this String content (which contains application/json). This string content is your access_token sent to you by LinkedIn. Once you get back the response, do this:
JSONParser parser = new JSONParser();
JSONObject o = (JSONObject) parser.parse(responseString); // responseString is the JSON that you got from LinkedIn.
System.out.println("Access Token: "+o.get("access_token")); //returns your access token
System.out.println("Expires: "+o.get("expires_in"));
Related
I have a Java web application running under Apache Tomcat (8.0.21.0). Its function is to monitor various external processes and display alerts and periodic updated statuses in a browser. The main HTTP request handler is simple enough.
public class MyApplication extends HttpServlet
{
.
.
.
public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
processRequest (request, response);
}
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
processRequest (request, response);
}
private static void processRequest (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
String strOption = request.getParameter ("option");
int nOption = Integer.parseInt (strOption);
response.setContentType ("text/html");
PrintWriter out = response.getWriter ();
outputPage (out, nOption);
}
private void outputPage (PrintWriter out, int nOption)
{
out.println ("<!DOCTYPE html>");
out.println ("<html>");
out.println ("<head>");
switch (nOption)
{
// title, style and <body> content depend on option passed in request
.
.
.
}
out.println ("</body>);
out.println ("</html>");
}
}
However, the application also includes a TCP Listener and socket, to receive IoT (Internet of Things) messages:
public class MyTCPconnection extends Thread
{
public Socket clientSocket; // socket created by listener
private String url = "[local host address and servlet name]";
.
.
.
public void run ()
{
int RC = 400; // default value = "Bad Request"
try
{
// get bytes from remote process
int receiveBufferSize = clientSocket.getReceiveBufferSize ();
byte[] receiveBuffer = new byte[receiveBufferSize];
int bytesRead = TCPreceive (clientSocket, receiveBuffer); // not shown
if (bytesRead != -1 && bytesRead != 0)
{
String strOption = getOption (receiveBuffer); // not shown
}
HttpPost httpPost = new HttpPost (url);
httpPost.setHeader ("Accept", "text/html,application/xhtml+xml,application/xml");
httpPost.setHeader ("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> requestParams = new ArrayList<NameValuePair>();
reqestParams.add (new BasicNameValuePair ("option", value));
CloseableHttpClient httpClient = HttpClients.createDefault ();
CloseableHttpResponse response = httpClient.execute (httpPost);
RC = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString (response.getEntity ());
system.out.println (responseBody);
}
catch (UnknownHostException e)
{
RC = 404;
}
catch (IOException e)
{
RC = 400;
}
TCPsend (clientSocket, RC); // reply to remote process, not shown
}
}
Take for granted that MyTCPconnection.run () generates a valid HTTP request body and submits a POST request to the main application. The problem I have encountered is that, where the POST is made from a web browser (IExplorer, Firefox etc), the application outputs a web page in the browser, but on receiving the POST request from the internal MyTCPconnection instance, it outputs nothing to any browser. Instead, it redirects the entire output to the responseBody.
I thought at first that I merely needed to save the HttpServletResponse and PrintWriter variables from a request from the browser, and pass the saved PrintWriter instance to the function outputPage. However, when I logged these, the results were:
Request from browser:
HttpServletResponse response = org.apache.catalina.connector.ResponseFacade#3e1d266b
PrintWriter out = org.apache.catalina.connector.CoyoteWriter#6bc55aa8
Request from MyTCPconnection.run ():
HttpServletResponse response = org.apache.catalina.connector.ResponseFacade#3e1d266b
PrintWriter out = org.apache.catalina.connector.CoyoteWriter#6bc55aa8
Any hints or hlp would be appreciated
I'm using Servlet to handle request and response.
I have used following code to Servlet my request to sublet using webservice:
JSONObject parans = new JSONObject();
parans.put("commandid", "Enamu7l");
System.out.println("parans = " + parans);
Client restClient = Client.create();
WebResource webResource = restClient.resource("URL");
ClientResponse resp = webResource.accept(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, parans.toJSONString());
Here is my servlet code to receive data.
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String commandid= request.getParameter("commandid");
System.out.println(commandid);
}
commandid recieve null from webservice.
What to do in webservice to get data in servlet?
WebResource not sending the data as part of the url, so you can not use request.getParameter. The data is send as request body using the post method.Read the data using the reader.
StringBuilder sb = new StringBuilder();
while ((s = request.getReader().readLine()) != null) {
sb.append(s);
}
JSONObject jSONObject = new JSONObject(sb.toString());
System.out.println(jSONObject.getString("commandid"));
You are sending JSON in request body, so you need to get it:
String json = request.getReader().lines().collect(Collectors.joining());
Convert to JSON:
JSONObject jsonObject = new JSONObject(json);
And get the value:
String value = jsonObject.getString("commandid");
I want to do a form POST request to the url,in case of valid response callback URL should be called(or hit) else should be redirected to another page of the same application.
Eg : While doing online payment, when we click on pay on any site,it is redirected to our mentioned url,if the user ain't logged in, user is redirected to login page.
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Error error = null;
String jNo=request.getParameter("jNo");
String emailId=request.getParameter("emailId");
String mobileNo=request.getParameter("mobileNo");
String callBackUrl=request.getParameter("url");
HttpResponse httpRespnse=null;
try {
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("https://localhost:8080/abc/xyz.aspx");
Invocation.Builder builder1 = webTarget.request(MediaType.APPLICATION_FORM_URLENCODED);
Form form = new Form();
form.param("jNo", jNo);
form.param("emailId", emailId);
form.param("url", callBackUrl);
form.param("mobileNo", mobileNo");
Response response1 = null;
response1 = builder1.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED));
String responseXml = response1.readEntity(String.class);
//PrintWriter out=new PrintWriter();
System.out.println(response1.getStatus());
} catch (Exception e) {
logger.error("Issue in Request" + e);
System.out.println(e.getMessage());
e.printStackTrace();
error = aadharProcess.getErrorMsg(Constants.ERR_CODE_INTERNAL_ERROR);
}
}
In a Java HttpServlet, is it possible to request data from another local service using the original request's header information without necessarily forwarding?
For example, I have FooBar.java:
// Handles the url at /foo/bar and can be accessed at http://localhost/foo/bar
public class FooBar extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Object data = ... // 1. Retrieve data at http://localhost/foo/baz utilizing the current request's header
Object newData = doSomething(data); // 2. Process the data
response.getWriter().write(newData.toString); // 3. Return the processed data
}
private Object doSomething(Object data)
{
// Perform some business logic
}
}
Step 1 is the issue here. The purpose of this is that I want to be able to perform some sort of logic on the data before returning it in full, but don't necessarily have access do make the changes on the handler at /foo/baz do to the propriety nature of things.
You can use this answer of me to create a HTTP Request: send get request
In addition, it may be necessary to copy the request header with some care:
private static final Set forbiddenCopyHeaders = new HashSet<>(Arrays.asList(new String[]{
"connection"
, "transfer-encoding"
, "content-length" // POST kann zu Status 500 führen, wenn die content-length kopiert wird
, "via"
, "x-forwarded-for"
, "x-forwarded-host"
, "x-forwarded-server"
}));
private void copyRequestHeaders(HttpServletRequest customerRequest, HttpRequestBase internRequest) throws
HttpException
{
Enumeration<String> headerNames = customerRequest.getHeaderNames();
String connectionHeader = customerRequest.getHeader("connection");
while (headerNames.hasMoreElements())
{
String headerName = headerNames.nextElement();
boolean copyAllowed = !forbiddenCopyHeaders.contains(headerName.toLowerCase()) &&
!StringUtils.containsIgnoreCase(connectionHeader, headerName);
if (copyAllowed)
{
Enumeration<String> values = customerRequest.getHeaders(headerName);
while (values.hasMoreElements())
{
internRequest.addHeader(headerName, values.nextElement());
}
}
}
setProxySpecificRequestHeaders(customerRequest, internRequest);
}
private void setProxySpecificRequestHeaders(HttpServletRequest customerRequest,
HttpRequestBase internRequest) throws HttpException
{
String serverHostName = "doorman";
try
{
serverHostName = InetAddress.getLocalHost().getHostName();
}
catch (UnknownHostException e)
{
logger.error("Couldn't get the hostname needed for headers x-forwarded-server and Via", e);
}
String originalVia = customerRequest.getHeader("via");
StringBuilder via = new StringBuilder("");
if (originalVia != null)
{
if (originalVia.contains(serverHostName))
{
logger.error("This proxy has already handled the Request, will abort.");
throw new HttpException("Request has a cyclic dependency on this proxy.");
}
else
{
via.append(originalVia).append(", ");
}
}
via.append(customerRequest.getProtocol()).append(" ").append(serverHostName);
internRequest.addHeader("via", via.toString());
internRequest.addHeader("x-forwarded-for", customerRequest.getRemoteAddr());
internRequest.addHeader("x-forwarded-host", customerRequest.getServerName());
internRequest.addHeader("x-forwarded-server", serverHostName);
internRequest.addHeader("accept-encoding", "");
}
Using HttpURLConnection and altering the header to include a property from the original request, I was able to get a BufferedReader from the HTTP request:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// Step 1
String serverName = request.getLocalName();
String contextPath = request.getContextPath();
URL url = new URL("https://" + serverName + contextPath + "/baz");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Key Header", request.getHeader("Key Header"));
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// Step 2
... // Do something with the data from the reader
// Step 3
... // Write the data back using the response
}
I have to write a controller in my project using servlets. I've done it before but I've never worked with AngularJS, so I did it via request.setAttribute() and request.getParameter() and put Java code inside of a JSP page. But now frontend developer used AngularJS and I have to return him a JSON object. And I have no idea how to do it. Here's the code of abTestCtrl.js:
app.controller("abTestCtrl", function($scope, $location, $http) {
$scope.title = "no title";
$scope.description = "no description";
$scope.getParam = $location.search()['id'];
if($scope.getParam === undefined)$scope.getParam = 0;
//$scope.getParam=2;
//path: localhost8080/UIUM.../servlet-name.java
//with two ids
//web.xml: serverlet mapping for the path
if($scope.getParam==='0'||$scope.getParam === 0){
var saveButton = document.getElementById("saveButton");
saveButton.classList.remove("hidden");
}
else{
$http.get('http://localhost:8080/UIUM_IMT4003/ABTestController', {command:'getTestCaseInfo', testcaseID:$scope.getParam}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
console.log('request succesful');
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log('request not succesful');
});
}
and my processRequest() code from the servlet:
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json; charset=UTF-8");
//PrintWriter printout = response.getWriter();
JSONObject jObject = null;
RequestDispatcher view = null;
TestcaseRepository testcaseRepo = new TestcaseRepository();
String command = request.getParameter("command");
if(command == null)
{
view = request.getRequestDispatcher("/testcases.jsp");
view.forward(request, response);
}
if(command.equals("getTestCaseInfo")){
String testcaseId = request.getParameter("testcaseID");
Testcase testcase = testcaseRepo.getTestcaseById(testcaseId);
jObject = new JSONObject();
jObject.put("id", testcaseId);
jObject.put("title", testcase.getTestcaseName());
jObject.put("testscenario", testcase.getTestcaseDescription());
// printout.print(jObject);
// printout.flush();
jObject.write(response.getWriter());
}
Can you please help me to process this request and finally return this poor JSON!
BTW, Servlet doesn't recognize command parameter. It gets null. But there is such parameter in AngularJS function.
Try using the javax.json.JsonObject as follow:
JsonObject jo=Json.createObjectBuilder()
.add("id", testcaseId)
.add("title", testcase.getTestcaseName())
.add("testscenario", testcase.getTestcaseDescription()).build();
Then set the response content type to json and send your json object in the response:
response.setContentType("application/json");// set content to json
PrintWriter out = response.getWriter();
out.print(jo);
out.flush();