Added api service
This commit is contained in:
@ -75,28 +75,11 @@
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework.social/spring-social-core -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.social</groupId>
|
||||
<artifactId>spring-social-core</artifactId>
|
||||
<version>1.1.6.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.social</groupId>
|
||||
<artifactId>spring-social-web</artifactId>
|
||||
<version>1.1.6.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.social</groupId>
|
||||
<artifactId>spring-social-config</artifactId>
|
||||
<version>1.1.6.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.social</groupId>
|
||||
<artifactId>spring-social-security</artifactId>
|
||||
<version>1.1.6.RELEASE</version>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.12</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@ -104,6 +87,17 @@
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.scribejava</groupId>
|
||||
<artifactId>scribejava-core</artifactId>
|
||||
<version>8.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.8</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -4,6 +4,21 @@
|
||||
"name": "plannaplan.email",
|
||||
"type": "java.lang.String",
|
||||
"description": "Email from which app sends message"
|
||||
},
|
||||
{
|
||||
"name": "plannaplan.apiurl",
|
||||
"type": "java.lang.String",
|
||||
"description": "Url to usos api endpoints"
|
||||
},
|
||||
{
|
||||
"name": "plannaplan.apikey",
|
||||
"type": "java.lang.String",
|
||||
"description": "Api consumer key"
|
||||
},
|
||||
{
|
||||
"name": "plannaplan.apisecret",
|
||||
"type": "java.lang.String",
|
||||
"description": "Api consumer secret"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
52
buisnesslogic/src/main/java/com/plannaplan/api/UsosOauth1Service.java
Executable file
52
buisnesslogic/src/main/java/com/plannaplan/api/UsosOauth1Service.java
Executable file
@ -0,0 +1,52 @@
|
||||
package com.plannaplan.api;
|
||||
|
||||
import com.github.scribejava.core.builder.api.DefaultApi10a;
|
||||
|
||||
public class UsosOauth1Service extends DefaultApi10a {
|
||||
|
||||
private static final String AUTHORIZE_URL = "https://usosapidemo.amu.edu.pl/services/oauth/authorize";
|
||||
private static final String REQUEST_TOKEN_URL = "https://usosapidemo.amu.edu.pl/services/oauth/request_token";
|
||||
|
||||
private final String scopesAsString;
|
||||
|
||||
protected UsosOauth1Service() {
|
||||
scopesAsString = null;
|
||||
}
|
||||
|
||||
protected UsosOauth1Service(String... scopes) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
for (String scope : scopes) {
|
||||
builder.append('+').append(scope);
|
||||
}
|
||||
scopesAsString = "?scope=" + builder.substring(1);
|
||||
}
|
||||
|
||||
private static class InstanceHolder {
|
||||
|
||||
private static final UsosOauth1Service INSTANCE = new UsosOauth1Service();
|
||||
}
|
||||
|
||||
public static UsosOauth1Service instance() {
|
||||
return InstanceHolder.INSTANCE;
|
||||
}
|
||||
|
||||
public static UsosOauth1Service instance(String... scopes) {
|
||||
return scopes == null || scopes.length == 0 ? instance() : new UsosOauth1Service(scopes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestTokenEndpoint() {
|
||||
return scopesAsString == null ? REQUEST_TOKEN_URL : REQUEST_TOKEN_URL + scopesAsString;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAccessTokenEndpoint() {
|
||||
return "https://usosapidemo.amu.edu.pl/services/oauth/access_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAuthorizationBaseUrl() {
|
||||
return AUTHORIZE_URL;
|
||||
}
|
||||
|
||||
}
|
29
buisnesslogic/src/main/java/com/plannaplan/models/UserApiResponse.java
Executable file
29
buisnesslogic/src/main/java/com/plannaplan/models/UserApiResponse.java
Executable file
@ -0,0 +1,29 @@
|
||||
package com.plannaplan.models;
|
||||
|
||||
/**
|
||||
* Model to keep data from /services/users/user response called in
|
||||
* UsosApiService
|
||||
*/
|
||||
public class UserApiResponse {
|
||||
private String name;
|
||||
private String surname;
|
||||
|
||||
public UserApiResponse() {
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public void setSurname(String surname) {
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -1,54 +1,74 @@
|
||||
package com.plannaplan.services;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.social.oauth1.OAuth1Parameters;
|
||||
import org.springframework.social.support.ClientHttpRequestFactorySelector;
|
||||
// import org.springframework.social.oauth1.OAuth1Template;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import com.github.scribejava.core.builder.ServiceBuilder;
|
||||
import com.github.scribejava.core.model.OAuth1AccessToken;
|
||||
import com.github.scribejava.core.model.OAuthRequest;
|
||||
import com.github.scribejava.core.model.Response;
|
||||
import com.github.scribejava.core.model.Verb;
|
||||
import com.github.scribejava.core.oauth.OAuth10aService;
|
||||
import com.plannaplan.api.UsosOauth1Service;
|
||||
import com.plannaplan.models.UserApiResponse;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.social.oauth1.*;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
// import org.springframework.social.oauth1.ProtectedResourceClientFactory;
|
||||
|
||||
@Service
|
||||
/**
|
||||
* service to call usos api endpoints
|
||||
*/
|
||||
public class UsosApiService {
|
||||
|
||||
private static final String NAME_FIELD = "first_name";
|
||||
private static final String SURNAME_FIELD = "last_name";
|
||||
|
||||
@Value("${plannaplan.apiurl}")
|
||||
private String apiUrl;
|
||||
|
||||
@Value("${plannaplan.apikey}")
|
||||
private String apikey;
|
||||
|
||||
@Value("${plannaplan.apisecret}")
|
||||
private String apisecret;
|
||||
|
||||
public UsosApiService() {
|
||||
|
||||
}
|
||||
|
||||
public void xd() {
|
||||
RestTemplate xddd = new RestTemplate();
|
||||
/**
|
||||
* /services/users/user
|
||||
*
|
||||
* @param usosId user id in usos
|
||||
* @return UserApiResponse modle contatining desired values
|
||||
*/
|
||||
public UserApiResponse getUserData(String usosId) {
|
||||
final UserApiResponse apiResponse = new UserApiResponse();
|
||||
try {
|
||||
final OAuth10aService service = new ServiceBuilder(apikey).apiSecret(apisecret)
|
||||
.build(UsosOauth1Service.instance());
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("oauth_version", "1.0");
|
||||
headers.set("oauth_signature_method", "HMAC-SHA1");
|
||||
headers.set("oauth_consumer_key", "e6jbeN57HC99MfsfmZwN");
|
||||
headers.set("oauth_signature", "fhcLdwerJyzZBGA9WnAYzbySJRW9Wv5wj3h8uVgp");
|
||||
headers.set("oauth_nonce", "" + (int) (Math.random() * 100000000));
|
||||
headers.set("oauth_timestamp", "" + (System.currentTimeMillis() / 1000));
|
||||
final OAuthRequest request = new OAuthRequest(Verb.GET, apiUrl + "/services/users/user?user_id=" + usosId);
|
||||
service.signRequest(new OAuth1AccessToken("", ""), request);
|
||||
|
||||
// BasicAuthenticationInterceptor
|
||||
try (Response response = service.execute(request)) {
|
||||
final String json = response.getBody();
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
Map<String, String> map = mapper.readValue(json, new TypeReference<Map<String, String>>() {
|
||||
});
|
||||
apiResponse.setName(map.get(NAME_FIELD));
|
||||
apiResponse.setSurname(map.get(SURNAME_FIELD));
|
||||
}
|
||||
} catch (IOException | InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// OAuth1Credentials credentials;
|
||||
// RestTemplate client = new
|
||||
// RestTemplate(ClientHttpRequestFactorySelector.getRequestFactory());
|
||||
// OAuth1RequestInterceptor interceptor = new
|
||||
// OAuth1RequestInterceptor(credentials);
|
||||
// List<ClientHttpRequestInterceptor> interceptors = new
|
||||
// LinkedList<ClientHttpRequestInterceptor>();
|
||||
// interceptors.add(interceptor);
|
||||
// client.setInterceptors(interceptors);
|
||||
|
||||
HttpEntity<String> entity = new HttpEntity<>(headers);
|
||||
|
||||
ResponseEntity<String> result = xddd.exchange("https://usosapidemo.amu.edu.pl/services/users/user",
|
||||
HttpMethod.GET, entity, String.class);
|
||||
System.out.println(result);
|
||||
return apiResponse;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,36 +2,37 @@ package com.plannaplan.services;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.MethodMode;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import com.plannaplan.entities.User;
|
||||
import com.plannaplan.exceptions.UserNotFoundException;
|
||||
import com.plannaplan.types.UserRoles;
|
||||
import com.plannaplan.models.UserApiResponse;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@ContextConfiguration
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
public class UsosApiServiceTest {
|
||||
|
||||
@Autowired
|
||||
private UsosApiService service;
|
||||
|
||||
@Test
|
||||
public void chuj() {
|
||||
this.service.xd();
|
||||
@Ignore
|
||||
public void shouldReturnPersonalData() throws IOException, InterruptedException, ExecutionException {
|
||||
|
||||
final UserApiResponse response = this.service.getUserData("499054");
|
||||
|
||||
assertTrue(response.getName().equals("Marcin"));
|
||||
assertTrue(response.getSurname().equals("Woźniak"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,5 +14,8 @@ spring.mail.properties.mail.smtp.auth=false
|
||||
spring.mail.properties.mail.smtp.starttls.enable=false
|
||||
|
||||
plannaplan.email = plannaplan.kontakt@gmail.com
|
||||
plannaplan.apiurl = https://usosapidemo.amu.edu.pl
|
||||
plannaplan.apikey=${PLANNAPLAN_CONSUMER_KEY}
|
||||
plannaplan.apisecret=${PLANNAPLAN_CONSUMER_SECRET}
|
||||
|
||||
server.port=1285
|
Reference in New Issue
Block a user