From 5b2bd7f928bbf9d3233ff029ed5c09ac46daf0de Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 28 Nov 2016 14:37:02 +0300 Subject: all components using jackson now, org.json serializer moved to compatibility tests package --- .../src/main/java/com/juick/www/FacebookLogin.java | 27 ++++++-------- .../src/main/java/com/juick/www/TwitterAuth.java | 10 +++-- .../main/java/com/juick/www/VKontakteLogin.java | 24 +++++------- .../main/java/com/juick/www/facebook/Graph.java | 43 ++++++++++++++++++++++ .../src/main/java/com/juick/www/twitter/User.java | 19 ++++++++++ .../main/java/com/juick/www/vk/GraphResponse.java | 19 ++++++++++ .../src/main/java/com/juick/www/vk/Token.java | 29 +++++++++++++++ juick-www/src/main/java/com/juick/www/vk/User.java | 39 ++++++++++++++++++++ 8 files changed, 177 insertions(+), 33 deletions(-) create mode 100644 juick-www/src/main/java/com/juick/www/facebook/Graph.java create mode 100644 juick-www/src/main/java/com/juick/www/twitter/User.java create mode 100644 juick-www/src/main/java/com/juick/www/vk/GraphResponse.java create mode 100644 juick-www/src/main/java/com/juick/www/vk/Token.java create mode 100644 juick-www/src/main/java/com/juick/www/vk/User.java (limited to 'juick-www') diff --git a/juick-www/src/main/java/com/juick/www/FacebookLogin.java b/juick-www/src/main/java/com/juick/www/FacebookLogin.java index 2b67dd5c..b42bb23b 100644 --- a/juick-www/src/main/java/com/juick/www/FacebookLogin.java +++ b/juick-www/src/main/java/com/juick/www/FacebookLogin.java @@ -17,8 +17,11 @@ */ package com.juick.www; +import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.server.UserQueries; -import org.json.JSONObject; +import com.juick.www.facebook.Graph; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.EmptyResultDataAccessException; @@ -43,10 +46,12 @@ public class FacebookLogin { private final String FACEBOOK_APPID; private final String FACEBOOK_SECRET; private final String FACEBOOK_REDIRECT = "http://juick.com/_fblogin"; + private final ObjectMapper mapper; public FacebookLogin(String ApplicationID, String secret) { this.FACEBOOK_APPID = ApplicationID; this.FACEBOOK_SECRET = secret; + mapper = new ObjectMapper(); } protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -94,24 +99,16 @@ public class FacebookLogin { } try { - JSONObject json = new JSONObject(graph); - String fbIDStr = json.getString("id"); - String fbName = json.getString("name"); - String fbLink = json.getString("link"); - boolean fbVerified = json.getBoolean("verified"); - - long fbID = 0; - if (fbIDStr != null && !fbIDStr.isEmpty()) { - fbID = Long.parseLong(fbIDStr); - } + Graph fb = mapper.readValue(graph, Graph.class); - if (fbID == 0 || fbName == null || fbLink == null || fbName.isEmpty() || fbLink.isEmpty()) { + long fbID = NumberUtils.toLong(fb.getId(), 0); + if (fbID == 0 || StringUtils.isBlank(fb.getName()) || StringUtils.isBlank(fb.getLink())) { throw new Exception(); } int uid = getUIDbyFBID(sql, fbID); if (uid > 0) { - if (!updateDB(sql, fbID, token, fbName, fbLink)) { + if (!updateDB(sql, fbID, token, fb.getName(), fb.getLink())) { throw new Exception(); } Cookie c = new Cookie("hash", UserQueries.getHashByUID(sql, uid)); @@ -119,9 +116,9 @@ public class FacebookLogin { response.addCookie(c); response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", "/"); - } else if (fbVerified) { + } else if (fb.getVerified()) { String loginhash = UUID.randomUUID().toString(); - if (!insertDB(sql, fbID, loginhash, token, fbName, fbLink)) { + if (!insertDB(sql, fbID, loginhash, token, fb.getName(), fb.getLink())) { throw new Exception(); } response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); diff --git a/juick-www/src/main/java/com/juick/www/TwitterAuth.java b/juick-www/src/main/java/com/juick/www/TwitterAuth.java index 5ea58eb3..c6aaf9a1 100644 --- a/juick-www/src/main/java/com/juick/www/TwitterAuth.java +++ b/juick-www/src/main/java/com/juick/www/TwitterAuth.java @@ -1,5 +1,6 @@ package com.juick.www; +import com.fasterxml.jackson.databind.ObjectMapper; import com.github.scribejava.apis.TwitterApi; import com.github.scribejava.core.builder.ServiceBuilder; import com.github.scribejava.core.model.OAuth1AccessToken; @@ -8,7 +9,6 @@ import com.github.scribejava.core.model.OAuthRequest; import com.github.scribejava.core.model.Verb; import com.github.scribejava.core.oauth.OAuth10aService; import com.juick.server.UserQueries; -import org.json.JSONObject; import org.springframework.jdbc.core.JdbcTemplate; import javax.servlet.ServletException; @@ -26,9 +26,12 @@ public class TwitterAuth { private String consumerKey, consumerSecret; + private final ObjectMapper mapper; + public TwitterAuth(String consumerKey, String consumerSecret) { this.consumerKey = consumerKey; this.consumerSecret = consumerSecret; + mapper = new ObjectMapper(); } protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) @@ -72,10 +75,9 @@ public class TwitterAuth { OAuth1AccessToken accessToken = oAuthService.getAccessToken(requestToken, verifier); OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, VERIFY_URL, oAuthService); oAuthService.signRequest(accessToken, oAuthRequest); - JSONObject jsonResponse = new JSONObject(oAuthRequest.send().getBody()); - String screenName = jsonResponse.getString("screen_name"); + com.juick.www.twitter.User twitterUser = mapper.readValue(oAuthRequest.send().getBody(), com.juick.www.twitter.User.class); if (UserQueries.linkTwitterAccount(sql, user, accessToken.getToken(), accessToken.getTokenSecret(), - screenName)) { + twitterUser.getScreenName())) { response.setStatus(HttpServletResponse.SC_FOUND); response.setHeader("Location", "http://juick.com/settings"); } else { diff --git a/juick-www/src/main/java/com/juick/www/VKontakteLogin.java b/juick-www/src/main/java/com/juick/www/VKontakteLogin.java index f0010ac8..83619b2a 100644 --- a/juick-www/src/main/java/com/juick/www/VKontakteLogin.java +++ b/juick-www/src/main/java/com/juick/www/VKontakteLogin.java @@ -17,9 +17,9 @@ */ package com.juick.www; +import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.server.UserQueries; -import org.json.JSONException; -import org.json.JSONObject; +import com.juick.www.vk.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.EmptyResultDataAccessException; @@ -43,6 +43,8 @@ public class VKontakteLogin { private static final String VK_SECRET = "z2afNI8jA5lIpZ2jsTm1"; private static final String VK_REDIRECT = "http://juick.com/_vklogin"; + private final ObjectMapper mapper = new ObjectMapper(); + protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String code = request.getParameter("code"); if (code == null || code.equals("")) { @@ -60,15 +62,9 @@ public class VKontakteLogin { } String token = null; long vkID = 0; - try { - JSONObject json = new JSONObject(tokenjson); - token = json.getString("access_token"); - vkID = json.getLong("user_id"); - } catch (JSONException e) { - logger.error("VK TOKEN EXCEPTION: ", e); - response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - return; - } + Token json = mapper.readValue(tokenjson, Token.class); + token = json.getAccessToken(); + vkID = json.getUserId(); if (token == null || vkID == 0) { logger.error("VK TOKEN EMPTY: " + tokenjson); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); @@ -85,9 +81,9 @@ public class VKontakteLogin { } try { - JSONObject json = new JSONObject(graph).getJSONArray("response").getJSONObject(0); - String vkName = json.getString("first_name") + " " + json.getString("last_name"); - String vkLink = json.getString("screen_name"); + com.juick.www.vk.User jsonUser = mapper.readValue(graph, GraphResponse.class).getUser(); + String vkName = jsonUser.getFirstName() + " " + jsonUser.getLastName(); + String vkLink = jsonUser.getScreenName(); if (vkName == null || vkLink == null || vkName.isEmpty() || vkName.length() == 1 || vkLink.isEmpty()) { throw new Exception(); diff --git a/juick-www/src/main/java/com/juick/www/facebook/Graph.java b/juick-www/src/main/java/com/juick/www/facebook/Graph.java new file mode 100644 index 00000000..b5827751 --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/facebook/Graph.java @@ -0,0 +1,43 @@ +package com.juick.www.facebook; + +/** + * Created by vitalyster on 28.11.2016. + */ +public class Graph { + private String id; + private String name; + private String link; + private boolean verified; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLink() { + return link; + } + + public void setLink(String link) { + this.link = link; + } + + public boolean getVerified() { + return verified; + } + + public void setVerified(boolean verified) { + this.verified = verified; + } +} diff --git a/juick-www/src/main/java/com/juick/www/twitter/User.java b/juick-www/src/main/java/com/juick/www/twitter/User.java new file mode 100644 index 00000000..47b90f9a --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/twitter/User.java @@ -0,0 +1,19 @@ +package com.juick.www.twitter; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Created by vitalyster on 28.11.2016. + */ +public class User { + private String screenName; + + @JsonProperty("screen_name") + public String getScreenName() { + return screenName; + } + + public void setScreenName(String screenName) { + this.screenName = screenName; + } +} diff --git a/juick-www/src/main/java/com/juick/www/vk/GraphResponse.java b/juick-www/src/main/java/com/juick/www/vk/GraphResponse.java new file mode 100644 index 00000000..f1985a91 --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/vk/GraphResponse.java @@ -0,0 +1,19 @@ +package com.juick.www.vk; + +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * Created by vitalyster on 28.11.2016. + */ +public class GraphResponse { + private User user; + + @JsonValue + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } +} diff --git a/juick-www/src/main/java/com/juick/www/vk/Token.java b/juick-www/src/main/java/com/juick/www/vk/Token.java new file mode 100644 index 00000000..abeb7c96 --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/vk/Token.java @@ -0,0 +1,29 @@ +package com.juick.www.vk; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Created by vitalyster on 28.11.2016. + */ +public class Token { + private Long userId; + private String accessToken; + + @JsonProperty("user_id") + public Long getUserId() { + return userId; + } + + public void setUserId(Long userId) { + this.userId = userId; + } + + @JsonProperty("access_token") + public String getAccessToken() { + return accessToken; + } + + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } +} diff --git a/juick-www/src/main/java/com/juick/www/vk/User.java b/juick-www/src/main/java/com/juick/www/vk/User.java new file mode 100644 index 00000000..c197da2e --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/vk/User.java @@ -0,0 +1,39 @@ +package com.juick.www.vk; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Created by vitalyster on 28.11.2016. + */ +public class User { + private String firstName; + private String lastName; + private String screenName; + + @JsonProperty("first_name") + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + @JsonProperty("last_name") + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + @JsonProperty("screen_name") + public String getScreenName() { + return screenName; + } + + public void setScreenName(String screenName) { + this.screenName = screenName; + } +} -- cgit v1.2.3