|
0
|
1 package de.mpiwg.monographs.servlet;
|
|
|
2
|
|
|
3 import java.io.IOException;
|
|
|
4
|
|
|
5 import javax.inject.Inject;
|
|
|
6 import javax.servlet.ServletException;
|
|
|
7 import javax.servlet.http.HttpServletRequest;
|
|
|
8 import javax.servlet.http.HttpServletResponse;
|
|
|
9
|
|
|
10 import org.apache.commons.lang.StringUtils;
|
|
|
11 import org.codehaus.jettison.json.JSONObject;
|
|
|
12 import org.swordapp.server.AuthCredentials;
|
|
|
13 import org.swordapp.server.SwordAuthException;
|
|
|
14 import org.swordapp.server.SwordServerException;
|
|
|
15
|
|
|
16 import edu.harvard.iq.dvn.api.datadeposit.SwordAuth;
|
|
|
17 import edu.harvard.iq.dvn.core.admin.VDCUser;
|
|
|
18
|
|
|
19 public class GetUser extends AbstractMonographServlet{
|
|
|
20
|
|
|
21 @Inject
|
|
|
22 private SwordAuth swordAuth;
|
|
|
23
|
|
|
24 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
25 // Set response content type
|
|
|
26 response.setContentType("application/json");
|
|
|
27 JSONObject jsonResponse = new JSONObject();
|
|
|
28
|
|
|
29 String user = request.getParameter("user");
|
|
|
30 String password = request.getParameter("password");
|
|
|
31
|
|
|
32
|
|
|
33 if(StringUtils.isNotEmpty(user) && StringUtils.isNotEmpty(password)){
|
|
|
34 AuthCredentials authCredentials = new AuthCredentials(user, password, null);
|
|
|
35 try {
|
|
|
36
|
|
|
37 VDCUser vdcUser = swordAuth.auth(authCredentials);
|
|
|
38
|
|
|
39
|
|
|
40 if(vdcUser != null){
|
|
|
41 JSONObject jsonUser = MonographUtils.jsonVDCUser(vdcUser);
|
|
|
42 jsonResponse.put("state", "ok");
|
|
|
43 jsonResponse.put("user", jsonUser);
|
|
|
44
|
|
|
45 }else{
|
|
|
46 jsonResponse.put("state", "error");
|
|
|
47 jsonResponse.put("error", "User " + user + " no found.");
|
|
|
48 }
|
|
|
49
|
|
|
50
|
|
|
51 } catch (SwordAuthException e) {
|
|
|
52 e.printStackTrace();
|
|
|
53 } catch (SwordServerException e) {
|
|
|
54 e.printStackTrace();
|
|
|
55 } catch (Exception e){
|
|
|
56 e.printStackTrace();
|
|
|
57 }
|
|
|
58 }else{
|
|
|
59 try {
|
|
|
60 jsonResponse.put("state", "error");
|
|
|
61 jsonResponse.put("error", "Request error. Parameters 'user' and 'password' are mandatory");
|
|
|
62 } catch (Exception e) {
|
|
|
63 e.printStackTrace();
|
|
|
64 }
|
|
|
65 }
|
|
|
66
|
|
|
67 java.io.PrintWriter out = response.getWriter();
|
|
|
68 out.print(jsonResponse);
|
|
|
69 out.flush();
|
|
|
70 }
|
|
|
71 }
|