1
|
package eu.dnetlib.openaire.user.api.services;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import com.unboundid.ldap.sdk.LDAPException;
|
5
|
import eu.dnetlib.openaire.user.MigrationUser;
|
6
|
import eu.dnetlib.openaire.user.dao.SQLMigrationUserDAO;
|
7
|
import eu.dnetlib.openaire.user.ldap.MUserActionsLDAP;
|
8
|
|
9
|
import javax.servlet.http.HttpServletRequest;
|
10
|
import javax.ws.rs.*;
|
11
|
import javax.ws.rs.core.Context;
|
12
|
import javax.ws.rs.core.MediaType;
|
13
|
import javax.ws.rs.core.Response;
|
14
|
import java.sql.SQLException;
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.List;
|
17
|
|
18
|
/**
|
19
|
* Created by sofia on 17/11/2016.
|
20
|
*/
|
21
|
@Path("/users")
|
22
|
public class Test2Service {
|
23
|
|
24
|
@GET
|
25
|
@Path("/{userId}")
|
26
|
@Produces(MediaType.APPLICATION_JSON)
|
27
|
public Response getUserById(@PathParam("userId") int userId)
|
28
|
{
|
29
|
SQLMigrationUserDAO mUserDAO = new SQLMigrationUserDAO();
|
30
|
try {
|
31
|
MigrationUser mUser = mUserDAO.fetchById(userId);
|
32
|
|
33
|
// Invalide user ID
|
34
|
if (mUser == null) {
|
35
|
String errorMessageJson = compose404Message("Cannot find user with id " + userId + ".");
|
36
|
|
37
|
return Response
|
38
|
.status(Response.Status.NOT_FOUND)
|
39
|
.entity(errorMessageJson)
|
40
|
.type(MediaType.APPLICATION_JSON)
|
41
|
.build();
|
42
|
}
|
43
|
|
44
|
return Response.status(200).entity(composeDataResponse(mUser)).build();
|
45
|
}
|
46
|
catch (SQLException e) {
|
47
|
return Response
|
48
|
.status(Response.Status.INTERNAL_SERVER_ERROR)
|
49
|
.entity(compose500Message("Fail to fetch users.", e))
|
50
|
.type(MediaType.APPLICATION_JSON)
|
51
|
.build();
|
52
|
}
|
53
|
}
|
54
|
|
55
|
@GET
|
56
|
@Path("/authenticate")
|
57
|
@Produces(MediaType.APPLICATION_JSON)
|
58
|
public Response authenticateUser(@QueryParam("username") String username, @QueryParam("password") String password)
|
59
|
{
|
60
|
SQLMigrationUserDAO mUserDAO = new SQLMigrationUserDAO();
|
61
|
try {
|
62
|
MigrationUser mUser = mUserDAO.fetchByUid(username);
|
63
|
|
64
|
// if user was not found
|
65
|
if (mUser == null) {
|
66
|
String errorMessageJson = compose404Message("Cannot find user with username " + username + ".");
|
67
|
|
68
|
return Response
|
69
|
.status(Response.Status.NOT_FOUND)
|
70
|
.entity(errorMessageJson)
|
71
|
.type(MediaType.APPLICATION_JSON)
|
72
|
.build();
|
73
|
}
|
74
|
|
75
|
boolean authenticated = MUserActionsLDAP.authenticateUser(mUser.getEmail(), password);
|
76
|
|
77
|
if (!authenticated) {
|
78
|
return Response
|
79
|
.status(Response.Status.UNAUTHORIZED)
|
80
|
.entity(compose401Message("User " + username + " could not be authenticated."))
|
81
|
.type(MediaType.APPLICATION_JSON)
|
82
|
.build();
|
83
|
}
|
84
|
return Response.status(200).entity(composeDataResponse(mUser)).build();
|
85
|
}
|
86
|
catch (LDAPException exc) {
|
87
|
return Response
|
88
|
.status(Response.Status.INTERNAL_SERVER_ERROR)
|
89
|
.entity(compose500Message("LDAP error.", exc))
|
90
|
.type(MediaType.APPLICATION_JSON)
|
91
|
.build();
|
92
|
}
|
93
|
catch (SQLException exc) {
|
94
|
return Response
|
95
|
.status(Response.Status.INTERNAL_SERVER_ERROR)
|
96
|
.entity(compose500Message("Fail to fetch users.", exc))
|
97
|
.type(MediaType.APPLICATION_JSON)
|
98
|
.build();
|
99
|
}
|
100
|
}
|
101
|
|
102
|
/* JSON Utility Methods */
|
103
|
|
104
|
private String compose401Message(String message) {
|
105
|
return "{ \"status\" : \"error\", \"code\" : \"401\", \"message\" : \" " + message +" \" }";
|
106
|
}
|
107
|
|
108
|
private String compose404Message(String message) {
|
109
|
return "{ \"status\" : \"error\", \"code\" : \"404\", \"message\" : \" " + message +" \" }";
|
110
|
}
|
111
|
|
112
|
private String compose500Message(String message, Exception exception) {
|
113
|
return "{ \"status\" : \"fail\", \"code\" : \"500\", \"message\" : \" " + message + "\", " +
|
114
|
"\"description\" : \""+ exception.getMessage() +"\" }";
|
115
|
}
|
116
|
|
117
|
private String composeDataResponse(MigrationUser user) {
|
118
|
return " { \"status\" : \"success\", \"code\": \"200\", " + "\"data\" : " + new Gson().toJson(user) + " }";
|
119
|
}
|
120
|
}
|