Guest User

Untitled

a guest
Jan 16th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. {
  2. "login-request":
  3. {
  4. "username":<insert username>,
  5. "password":<insert password>
  6. }
  7. }
  8.  
  9. {
  10. "login-token":
  11. {
  12. "token":<insert token>
  13. }
  14. }
  15.  
  16. Client client = ClientBuilder.newClient();
  17.  
  18. WebTarget target = client.target("https://127.0.0.1:8443/").path(appName + "/logins");
  19.  
  20. //build JSON Object
  21. HashMap<String, String> userMap = new HashMap<>();
  22. userMap.put("username", user.getUsername());
  23. userMap.put("password", user.getPassword());
  24.  
  25. //JSON logins request!
  26. JSONObject jsonLoginRequest = new JSONObject();
  27. try {
  28. jsonLoginRequest.put("login-request", userMap);
  29. } catch (JSONException e) {
  30. e.printStackTrace();
  31. }
  32.  
  33. return target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(jsonLoginRequest.toString()));
  34.  
  35. @POST
  36. @Path("logins")
  37. @Consumes(MediaType.APPLICATION_JSON)
  38. @Produces(MediaType.APPLICATION_JSON)
  39. public Response generateToken(@Context HttpServletRequest request) {
  40.  
  41. if (request != null) {
  42. User user;
  43.  
  44. DBHandshaker handshaker = DBHandshaker.getInstance();
  45. user = handshaker.logUser(request.getParameter("username"), request.getParameter("password"));
  46.  
  47. if (user != null) {
  48. StringUtil stringUtil = StringUtil.getInstance();
  49. String tokenString = stringUtil.encryptForToken(user.getUsername(), user.getPassword());
  50.  
  51. HashMap<String, String> tokenMap = new HashMap<>();
  52. tokenMap.put("token", tokenString);
  53.  
  54. JSONObject jsonLoginResponse = new JSONObject();
  55. try {
  56. jsonLoginResponse.put("login-token", tokenMap);
  57. } catch (JSONException e) {
  58. e.printStackTrace();
  59. }
  60.  
  61. return Response.ok(jsonLoginResponse.toString(), MediaType.APPLICATION_JSON).build();
  62.  
  63. } else {
  64. return Response.status(Response.Status.UNAUTHORIZED).build();
  65. }
  66.  
  67. } else {
  68. return Response.status(Response.Status.NO_CONTENT).build();
  69. }
  70.  
  71. }
  72.  
  73. LoginParser loginParser = new LoginParser();
  74. Response response = loginParser.parseRequest(username, password);
  75.  
  76. boolean isValidLogin = Response.Status.OK.getStatusCode() == response.getStatusInfo().getStatusCode();
  77.  
  78. if (isValidLogin) {
  79.  
  80. // Store the current username in the service session
  81. getSession().setAttribute("user", username);
  82.  
  83. HttpEntity entity = (HttpEntity) response.getEntity();
  84. try {
  85. String retSRc = EntityUtils.toString(entity);
  86. JSONObject jsonObject = new JSONObject(retSRc);
  87.  
  88. System.out.println(jsonObject);
  89.  
  90. } catch (IOException | JSONException e) {
  91. e.printStackTrace();
  92. }
  93.  
  94. // Navigate to main view
  95. getUI().getNavigator().navigateTo(LoginMainView.NAME);//
  96.  
  97. }
  98.  
  99. package net.test;
  100.  
  101. import java.util.HashMap;
  102.  
  103. import org.codehaus.jettison.json.JSONException;
  104. import org.codehaus.jettison.json.JSONObject;
  105.  
  106. import com.sun.jersey.api.client.Client;
  107. import com.sun.jersey.api.client.ClientResponse;
  108. import com.sun.jersey.api.client.WebResource;
  109.  
  110. public class Consumer {
  111. public static void main(String[] args) {
  112. Client client = Client.create();
  113. ClientResponse clientResponse = null;
  114.  
  115. User u = new User();
  116. u.setUsername("hello");
  117. u.setPassword("hello");
  118. WebResource webResource = client
  119. .resource("http://localhost:8080/"+appName"+/logins");
  120. HashMap<String, String> userMap = new HashMap<String, String>();
  121. userMap.put("username", u.getUsername());
  122. userMap.put("password", u.getPassword());
  123. // JSON logins request!
  124. JSONObject jsonLoginRequest = new JSONObject();
  125. try {
  126. jsonLoginRequest.put("login-request", userMap);
  127. System.out.println(jsonLoginRequest.get("login-request"));
  128. System.out.println(jsonLoginRequest.getJSONObject("login-request")
  129. .get("username"));
  130. } catch (JSONException e) {
  131. e.printStackTrace();
  132. }
  133. // POST Operation
  134. clientResponse = webResource.post(ClientResponse.class,
  135. jsonLoginRequest);
  136. System.out.println(jsonLoginRequest);
  137. // Send the return value in object
  138. String result = clientResponse.getEntity(String.class);
  139. System.out.println(result);
  140. }
  141. }
  142.  
  143. package net.test;
  144. //Server Processing code
  145. import java.util.HashMap;
  146. import java.util.Random;
  147.  
  148. import javax.ws.rs.Consumes;
  149. import javax.ws.rs.POST;
  150. import javax.ws.rs.Path;
  151. import javax.ws.rs.Produces;
  152. import javax.ws.rs.core.Context;
  153. import javax.ws.rs.core.MediaType;
  154. import javax.ws.rs.core.Response;
  155.  
  156. import org.codehaus.jettison.json.JSONException;
  157. import org.codehaus.jettison.json.JSONObject;
  158.  
  159. @Path("/")
  160. public class Producer {
  161. @POST
  162. @Path("logins")
  163. @Consumes(MediaType.APPLICATION_JSON)
  164. @Produces(MediaType.APPLICATION_JSON)
  165. public Response generateToken(String objInputData) {
  166.  
  167. JSONObject jsonObject = null;
  168. User user = null;
  169. String username = null;
  170. String password = null;
  171. try {
  172. jsonObject = new JSONObject(objInputData);
  173. user = new User();
  174. username = jsonObject.getJSONObject("login-request")
  175. .get("username").toString();
  176. password = jsonObject.getJSONObject("login-request")
  177. .get("password").toString();
  178. // Your business logic goes here
  179. // DBHandshaker handshaker = DBHandshaker.getInstance();
  180. // user = handshaker.logUser(username, password);
  181. System.out.println(username+password);
  182. if (user != null) {
  183. // write your logic to generate token
  184. // StringUtil stringUtil = StringUtil.getInstance();
  185. // String tokenString =
  186. // stringUtil.encryptForToken(user.getUsername(),
  187. // user.getPassword());
  188. // dummy string to test
  189. String tokenString = "Helllllllllgjdkg";
  190.  
  191. HashMap<String, String> tokenMap = new HashMap<String, String>();
  192. tokenMap.put("token", tokenString);
  193.  
  194. JSONObject jsonLoginResponse = new JSONObject();
  195. try {
  196. jsonLoginResponse.put("login-token", tokenMap);
  197. } catch (JSONException e) {
  198. e.printStackTrace();
  199. }
  200.  
  201. return Response.ok(jsonLoginResponse.toString(),
  202. MediaType.APPLICATION_JSON).build();
  203.  
  204. } else {
  205. return Response.status(Response.Status.UNAUTHORIZED).build();
  206. }
  207. } catch (Exception e1) {
  208. return Response.status(Response.Status.NO_CONTENT).build();
  209. }
  210. }
  211. }
Add Comment
Please, Sign In to add comment