1 | /* |
---|
2 | * Copyright (C) 2013 DASISH |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU General Public License |
---|
6 | * as published by the Free Software Foundation; either version 2 |
---|
7 | * of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
17 | */ |
---|
18 | package eu.dasish.annotation.backend.rest; |
---|
19 | |
---|
20 | import eu.dasish.annotation.backend.dao.DBIntegrityService; |
---|
21 | import eu.dasish.annotation.schema.AnnotationInfoList; |
---|
22 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
23 | import java.io.BufferedReader; |
---|
24 | import java.io.FileReader; |
---|
25 | import java.io.IOException; |
---|
26 | import java.util.ArrayList; |
---|
27 | import java.util.List; |
---|
28 | import java.util.UUID; |
---|
29 | import javax.servlet.ServletContext; |
---|
30 | import javax.servlet.http.HttpServletRequest; |
---|
31 | import javax.servlet.http.HttpServletResponse; |
---|
32 | import javax.ws.rs.GET; |
---|
33 | import javax.ws.rs.PUT; |
---|
34 | import javax.ws.rs.Path; |
---|
35 | import javax.ws.rs.PathParam; |
---|
36 | import javax.ws.rs.Produces; |
---|
37 | import javax.ws.rs.core.Context; |
---|
38 | import javax.ws.rs.core.MediaType; |
---|
39 | import javax.ws.rs.core.UriInfo; |
---|
40 | import javax.xml.bind.JAXBElement; |
---|
41 | import org.slf4j.Logger; |
---|
42 | import org.slf4j.LoggerFactory; |
---|
43 | import org.springframework.beans.factory.annotation.Autowired; |
---|
44 | import org.springframework.stereotype.Component; |
---|
45 | import org.springframework.transaction.annotation.Transactional; |
---|
46 | |
---|
47 | /** |
---|
48 | * |
---|
49 | * @author olhsha |
---|
50 | */ |
---|
51 | @Component |
---|
52 | @Path("/debug") |
---|
53 | public class DebugResource { |
---|
54 | |
---|
55 | @Autowired |
---|
56 | private DBIntegrityService dbIntegrityService; |
---|
57 | @Context |
---|
58 | private HttpServletRequest httpServletRequest; |
---|
59 | @Context |
---|
60 | private HttpServletResponse httpServletResponse; |
---|
61 | @Context |
---|
62 | private UriInfo uriInfo; |
---|
63 | @Context |
---|
64 | private ServletContext context; |
---|
65 | final String default_permission = "reader"; |
---|
66 | public static final Logger loggerServer = LoggerFactory.getLogger(HttpServletResponse.class); |
---|
67 | private final VerboseOutput verboseOutput = new VerboseOutput(httpServletResponse, loggerServer); |
---|
68 | private final String admin = "admin"; |
---|
69 | private final String developer = "developer"; |
---|
70 | |
---|
71 | @GET |
---|
72 | @Produces(MediaType.TEXT_XML) |
---|
73 | @Path("annotations") |
---|
74 | @Transactional(readOnly = true) |
---|
75 | public JAXBElement<AnnotationInfoList> getAllAnnotations() throws IOException { |
---|
76 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
77 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
78 | Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
79 | if (userID != null) { |
---|
80 | String typeOfAccount = dbIntegrityService.getTypeOfUserAccount(userID); |
---|
81 | if (typeOfAccount.equals(admin) || typeOfAccount.equals(developer)) { |
---|
82 | final AnnotationInfoList annotationInfoList = dbIntegrityService.getAllAnnotationInfos(); |
---|
83 | return new ObjectFactory().createAnnotationInfoList(annotationInfoList); |
---|
84 | } else { |
---|
85 | verboseOutput.DEVELOPER_RIGHTS_EXPECTED(); |
---|
86 | } |
---|
87 | } else { |
---|
88 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
89 | } |
---|
90 | return new ObjectFactory().createAnnotationInfoList(new AnnotationInfoList()); |
---|
91 | } |
---|
92 | |
---|
93 | @GET |
---|
94 | @Produces(MediaType.TEXT_PLAIN) |
---|
95 | @Path("/logDatabase/{n}") |
---|
96 | @Transactional(readOnly = true) |
---|
97 | public String getDasishBackendLog(@PathParam("n") int n) throws IOException { |
---|
98 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
99 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
100 | Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
101 | if (userID != null) { |
---|
102 | String typeOfAccount = dbIntegrityService.getTypeOfUserAccount(userID); |
---|
103 | if (typeOfAccount.equals(admin) || typeOfAccount.equals(developer)) { |
---|
104 | return logFile("eu.dasish.annotation.backend.logDatabaseLocation", n); |
---|
105 | } else { |
---|
106 | verboseOutput.DEVELOPER_RIGHTS_EXPECTED(); |
---|
107 | } |
---|
108 | } else { |
---|
109 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
110 | } |
---|
111 | return " "; |
---|
112 | } |
---|
113 | |
---|
114 | @GET |
---|
115 | @Produces(MediaType.TEXT_PLAIN) |
---|
116 | @Path("/remoteID") |
---|
117 | @Transactional(readOnly = true) |
---|
118 | public String getLoggedInRemoteID() throws IOException { |
---|
119 | return httpServletRequest.getRemoteUser(); |
---|
120 | } |
---|
121 | |
---|
122 | ///// |
---|
123 | @GET |
---|
124 | @Produces(MediaType.TEXT_PLAIN) |
---|
125 | @Path("/logServer/{n}") |
---|
126 | @Transactional(readOnly = true) |
---|
127 | public String getDasishServerLog(@PathParam("n") int n) throws IOException { |
---|
128 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
129 | Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
130 | if (userID != null) { |
---|
131 | String typeOfAccount = dbIntegrityService.getTypeOfUserAccount(userID); |
---|
132 | if (typeOfAccount.equals(admin) || typeOfAccount.equals(developer)) { |
---|
133 | return logFile("eu.dasish.annotation.backend.logServerLocation", n); |
---|
134 | } else { |
---|
135 | verboseOutput.DEVELOPER_RIGHTS_EXPECTED(); |
---|
136 | } |
---|
137 | } else { |
---|
138 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
139 | } |
---|
140 | return " "; |
---|
141 | |
---|
142 | } |
---|
143 | |
---|
144 | ////////////////////////////////// |
---|
145 | @PUT |
---|
146 | @Produces(MediaType.TEXT_XML) |
---|
147 | @Path("account/{userId}/make/{account}") |
---|
148 | @Transactional(readOnly = true) |
---|
149 | public String updateUsersAccount(@PathParam("userId") String userId, @PathParam("account") String account) throws IOException { |
---|
150 | dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString()); |
---|
151 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
152 | Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser); |
---|
153 | if (userID != null) { |
---|
154 | String typeOfAccount = dbIntegrityService.getTypeOfUserAccount(userID); |
---|
155 | if (typeOfAccount.equals(admin)) { |
---|
156 | final boolean update = dbIntegrityService.updateAccount(UUID.fromString(userId), account); |
---|
157 | return (update ? "The account is updated" : "The account is not updated, see the log."); |
---|
158 | } else { |
---|
159 | verboseOutput.ADMIN_RIGHTS_EXPECTED(); |
---|
160 | } |
---|
161 | } else { |
---|
162 | verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser); |
---|
163 | } |
---|
164 | return " "; |
---|
165 | } |
---|
166 | |
---|
167 | /////////////////////////////////////////////////// |
---|
168 | private String logFile(String location, int n) throws IOException { |
---|
169 | BufferedReader reader = new BufferedReader(new FileReader(context.getInitParameter(location))); |
---|
170 | List<String> lines = new ArrayList<String>(); |
---|
171 | StringBuilder result = new StringBuilder(); |
---|
172 | int i = 0; |
---|
173 | String line; |
---|
174 | while ((line = reader.readLine()) != null) { |
---|
175 | lines.add(line); |
---|
176 | i++; |
---|
177 | } |
---|
178 | // want to read the last n rows, i.e. the rows (i-1), (i-1-1),...,(i-1-(n-1)) |
---|
179 | int last = (i > n) ? (i - n) : 0; |
---|
180 | for (int j = i - 1; j >= last; j--) { |
---|
181 | result.append(lines.get(j)).append("\n"); |
---|
182 | } |
---|
183 | return result.toString(); |
---|
184 | } |
---|
185 | } |
---|