Skip to content

Commit

Permalink
Downgraded required Java version from 1.7 to 1.6
Browse files Browse the repository at this point in the history
Upgraded dependencies to: jackson, spring, spring-social, spring-security
Added support for retrieving e-mail along with profile.
  • Loading branch information
JNO committed Jun 28, 2016
1 parent 0950e30 commit 1b0fbd2
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 41 deletions.
26 changes: 18 additions & 8 deletions spring-social-live/pom.xml
Expand Up @@ -47,18 +47,18 @@

<servlet.api.version>2.5</servlet.api.version>
<!-- Spring -->
<spring.social.version>1.1.0.M4</spring.social.version>
<spring.framework.version>3.2.8.RELEASE</spring.framework.version>
<spring.security.version>3.2.3.RELEASE</spring.security.version>
<spring.social.version>1.1.4.RELEASE</spring.social.version>
<spring.framework.version>4.2.6.RELEASE</spring.framework.version>
<spring.security.version>3.2.5.RELEASE</spring.security.version>

<!-- Jackson - Java JSON Parser and Data Binder -->
<jackson.version>1.9.13</jackson.version>
<jackson.version>2.6.4</jackson.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven-gpg-plugin.version>1.4</maven-gpg-plugin.version>
<maven-compiler-plugin.java.source.version>1.7</maven-compiler-plugin.java.source.version>
<maven-compiler-plugin.java.target.version>1.7</maven-compiler-plugin.java.target.version>
<maven-compiler-plugin.java.source.version>1.6</maven-compiler-plugin.java.source.version>
<maven-compiler-plugin.java.target.version>1.6</maven-compiler-plugin.java.target.version>
<maven-compiler-plugin.version>3.0</maven-compiler-plugin.version>
</properties>

Expand Down Expand Up @@ -146,8 +146,18 @@

<!-- Jackson JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>

Expand Down
@@ -1,6 +1,7 @@
package org.springframework.social.live.api;

import org.codehaus.jackson.annotate.JsonProperty;

import com.fasterxml.jackson.annotation.JsonProperty;

public class LiveProfile {

Expand All @@ -15,6 +16,8 @@ public class LiveProfile {
private final String lastName;

private final String gender;

private final String email;

private String link;

Expand All @@ -23,12 +26,13 @@ public class LiveProfile {
@JsonProperty("updated_time")
private String updatedTime;

public LiveProfile(String id, String name, String firstName, String lastName, String gender, String locale) {
public LiveProfile(String id, String name, String firstName, String lastName, String gender, String email, String locale) {
this.id = id;
this.name = name;
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.email = email;
}

public String getLocale() {
Expand All @@ -55,6 +59,10 @@ public String getLastName() {
return lastName;
}

public String getEmail() {
return email;
}

public String getGender() {
return gender;
}
Expand Down
@@ -1,18 +1,17 @@
package org.springframework.social.live.api.impl;

import java.util.Map;

import org.springframework.social.live.api.LiveProfile;
import org.springframework.social.live.api.UserOperations;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class UserTemplate extends AbstractLiveOperations implements UserOperations {
private final RestTemplate restTemplate;

public UserTemplate(LiveTemplate liveTemplate, RestTemplate restTemplate, boolean authorized) {
super(authorized);
this.restTemplate = restTemplate;

}

@Override
Expand All @@ -25,8 +24,23 @@ public LiveProfile getUserProfile() {
String lastName = String.valueOf(user.get("last_name"));
String gender = String.valueOf(user.get("gender"));
String locale = String.valueOf(user.get("locale"));

return new LiveProfile(id, name, firstName, lastName, gender, locale);
String email = null;
//if requested scope was wl.emails, this field will be present
Map<String, String> emails = (Map<String, String>)user.get("emails");
if (emails != null) {
//grab preferred email
if (emails.containsKey("preferred")) {
email = emails.get("preferred");
//ok grab account related email
} else if (emails.containsKey("account")) {
email = emails.get("account");
//ok grab what you have then :)
} else if (!emails.isEmpty()) {
email = emails.values().iterator().next();
}
}

return new LiveProfile(id, name, firstName, lastName, gender, email, locale);

}

Expand Down
@@ -1,15 +1,14 @@
package org.springframework.social.live.api.onedrive.impl;

import java.util.List;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.social.live.api.impl.AbstractLiveOperations;
import org.springframework.social.live.api.onedrive.FriendlyNameOperations;
import org.springframework.social.live.api.onedrive.Metadata;
import org.springframework.web.client.RestTemplate;

import java.util.List;

public class FriendlyNameTemplate extends AbstractLiveOperations implements FriendlyNameOperations {

private final RestTemplate restTemplate;
Expand Down Expand Up @@ -72,10 +71,11 @@ public List<Metadata> getSharedDocuments() {
private List<Metadata> listOfMetadata(String path) {

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = restTemplate.getForObject(buildUri(path), JsonNode.class);
String json = restTemplate.getForObject(buildUri(path), String.class);

try {
return objectMapper.readValue(jsonNode, new TypeReference<List<Metadata>>() {});
JavaType type = objectMapper.getTypeFactory().constructCollectionType(List.class, Metadata.class);
return objectMapper.readValue(json, type);
} catch(Exception ex) {
throw new RuntimeException(ex);
}
Expand Down
@@ -1,11 +1,7 @@
package org.springframework.social.live.api.onedrive.impl;

import java.net.URI;
import java.util.List;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.social.live.api.impl.AbstractLiveOperations;
import org.springframework.social.live.api.onedrive.FilterType;
import org.springframework.social.live.api.onedrive.FriendlyNameOperations;
Expand All @@ -16,6 +12,9 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

public class OneDriveTemplate extends AbstractLiveOperations implements OneDriveOperations {

private final RestTemplate restTemplate;
Expand Down Expand Up @@ -70,10 +69,11 @@ public List<Metadata> getMetadata(String folderID, int limit, int offset) {
private List<Metadata> listOfMetadata(URI uri) {

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = restTemplate.getForObject(uri, JsonNode.class);
String json = restTemplate.getForObject(uri, String.class);

try {
return objectMapper.readValue(jsonNode, new TypeReference<List<Metadata>>() {});
JavaType type = objectMapper.getTypeFactory().constructCollectionType(List.class, Metadata.class);
return objectMapper.readValue(json, type);
} catch(Exception ex) {
throw new RuntimeException(ex);
}
Expand Down
@@ -1,22 +1,42 @@
package org.springframework.social.live.config.annotation;

import org.springframework.social.config.annotation.AbstractProviderConfigRegistrarSupport;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.social.config.support.ProviderConfigurationSupport;
import org.springframework.social.live.config.LiveApiHelper;
import org.springframework.social.live.connect.LiveConnectionFactory;
import org.springframework.social.live.security.LiveAuthenticationService;
import org.springframework.social.security.provider.SocialAuthenticationService;

public class LiveProviderConfigRegistrar extends
AbstractProviderConfigRegistrarSupport {
import java.lang.annotation.Annotation;
import java.util.Map;

public class LiveProviderConfigRegistrar extends ProviderConfigurationSupport {
private final Class<? extends Annotation> providerConfigAnnotation;

public LiveProviderConfigRegistrar() {

super(EnableLive.class,LiveConnectionFactory.class,LiveApiHelper.class);
super(LiveConnectionFactory.class, LiveApiHelper.class);
this.providerConfigAnnotation = EnableLive.class;
}

@Override
protected Class<? extends SocialAuthenticationService<?>> getAuthenticationServiceClass() {

return LiveAuthenticationService.class;
}

public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
Map<String, Object> allAttributes = metadata.getAnnotationAttributes(providerConfigAnnotation.getName());
registerBeanDefinitions(registry, allAttributes);
}

@Override
protected String getAppId(Map<String, Object> allAttributes) {
return (String) allAttributes.get("appId");
}

@Override
protected String getAppSecret(Map<String, Object> allAttributes) {
return (String) allAttributes.get("appSecret");
}

}
Expand Up @@ -10,29 +10,37 @@

public class LiveAdapter implements ApiAdapter<Live> {

@Override
public boolean test(Live live) {
try {
live.userOperations().getUserProfile();
return true;
}
catch (ApiException e) {
} catch(ApiException e) {
return false;
}
}

@Override
public void setConnectionValues(Live live, ConnectionValues values) {
LiveProfile profile = live.userOperations().getUserProfile();
values.setProviderUserId(profile.getId());
values.setDisplayName(profile.getName());
values.setImageUrl("https://apis.live.net/v5.0/" + profile.getId() + "/picture");
}

@Override
public UserProfile fetchUserProfile(Live live) {
LiveProfile profile = live.userOperations().getUserProfile();
return new UserProfileBuilder().setName(profile.getName()).setFirstName(profile.getFirstName())
.setLastName(profile.getLastName()).setUsername(profile.getId()).build();
return new UserProfileBuilder()
.setName(profile.getName())
.setFirstName(profile.getFirstName())
.setLastName(profile.getLastName())
.setUsername(profile.getId())
.setEmail(profile.getEmail())
.build();
}

@Override
public void updateStatus(Live live, String message) {
// not yet implemented
}
Expand Down
Expand Up @@ -28,7 +28,7 @@ protected RestTemplate createRestTemplate() {
RestTemplate template = super.createRestTemplate();
for (HttpMessageConverter<?> converter : template.getMessageConverters()){
if(converter instanceof MappingJackson2HttpMessageConverter){
List<MediaType> supportedMediaTypes = new ArrayList<>();
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
// Add all default
supportedMediaTypes.addAll(converter.getSupportedMediaTypes());
// And also handle text/html json returned on POST to /token
Expand Down
Expand Up @@ -10,6 +10,7 @@ public LiveServiceProvider(String clientId, String clientSecret) {
super(new LiveOAuth2Template(clientId, clientSecret));
}

@Override
public Live getApi(String accessToken) {
return new LiveTemplate(accessToken);
}
Expand Down

0 comments on commit 1b0fbd2

Please sign in to comment.