source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/DebugResource.java @ 5468

Last change on this file since 5468 was 5468, checked in by olhsha@mpi.nl, 10 years ago

adding three API's: update cached's blob via sending the path/url to its file, updating a resource's external UUID (only for the admin), generating random uuid.

File size: 8.2 KB
Line 
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 */
18package eu.dasish.annotation.backend.rest;
19
20import eu.dasish.annotation.backend.BackendConstants;
21import eu.dasish.annotation.backend.Helpers;
22import eu.dasish.annotation.backend.NotInDataBaseException;
23import eu.dasish.annotation.backend.Resource;
24import eu.dasish.annotation.schema.AnnotationInfoList;
25import eu.dasish.annotation.schema.ObjectFactory;
26import java.io.BufferedReader;
27import java.io.FileReader;
28import java.io.IOException;
29import java.util.ArrayList;
30import java.util.List;
31import java.util.UUID;
32import javax.servlet.http.HttpServletResponse;
33import javax.ws.rs.GET;
34import javax.ws.rs.PUT;
35import javax.ws.rs.Path;
36import javax.ws.rs.PathParam;
37import javax.ws.rs.Produces;
38import javax.ws.rs.core.MediaType;
39import javax.xml.bind.JAXBElement;
40import org.springframework.stereotype.Component;
41import org.springframework.transaction.annotation.Transactional;
42
43/**
44 *
45 * @author olhsha
46 */
47@Component
48@Path("/debug")
49public class DebugResource extends ResourceResource {
50
51    private final String developer = "developer";
52   
53   
54    @GET
55    @Produces(MediaType.TEXT_PLAIN)
56    @Path("uuid")
57    public String generateUUID() throws IOException {
58       return (Helpers.generateUUID()).toString();
59    }
60
61    @GET
62    @Produces(MediaType.TEXT_XML)
63    @Path("annotations")
64    @Transactional(readOnly = true)
65    public JAXBElement<AnnotationInfoList> getAllAnnotations() throws IOException {
66        Number remotePrincipalID = this.getPrincipalID();
67        if (remotePrincipalID == null) {
68            return new ObjectFactory().createAnnotationInfoList(new AnnotationInfoList());
69        }
70        String typeOfAccount = dbDispatcher.getTypeOfPrincipalAccount(remotePrincipalID);
71        if (typeOfAccount.equals(admin) || typeOfAccount.equals(developer)) {
72            final AnnotationInfoList annotationInfoList = dbDispatcher.getAllAnnotationInfos();
73            return new ObjectFactory().createAnnotationInfoList(annotationInfoList);
74        } else {
75            this.DEVELOPER_RIGHTS_EXPECTED();
76            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
77            return new ObjectFactory().createAnnotationInfoList(new AnnotationInfoList());
78        }
79    }
80
81    @GET
82    @Produces(MediaType.TEXT_PLAIN)
83    @Path("/logDatabase/{n}")
84    @Transactional(readOnly = true)
85    public String getDasishBackendLog(@PathParam("n") int n) throws IOException {
86        Number remotePrincipalID = this.getPrincipalID();
87        if (remotePrincipalID == null) {
88            return " ";
89        }
90        String typeOfAccount = dbDispatcher.getTypeOfPrincipalAccount(remotePrincipalID);
91        if (typeOfAccount.equals(admin) || typeOfAccount.equals(developer)) {
92            return logFile("eu.dasish.annotation.backend.logDatabaseLocation", n);
93        } else {
94            this.DEVELOPER_RIGHTS_EXPECTED();
95            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
96            return "Coucou.";
97        }
98    }
99
100    @GET
101    @Produces(MediaType.TEXT_PLAIN)
102    @Path("/remoteID")
103    @Transactional(readOnly = true)
104    public String getLoggedInRemoteID() {
105        return (httpServletRequest.getRemoteUser() != null) ? httpServletRequest.getRemoteUser() : "Null";
106    }
107
108    /////
109    @GET
110    @Produces(MediaType.TEXT_PLAIN)
111    @Path("/logServer/{n}")
112    @Transactional(readOnly = true)
113    public String getDasishServerLog(@PathParam("n") int n) throws IOException {
114        Number remotePrincipalID = this.getPrincipalID();
115        if (remotePrincipalID == null) {
116            return " ";
117        }
118        String typeOfAccount = dbDispatcher.getTypeOfPrincipalAccount(remotePrincipalID);
119        if (typeOfAccount.equals(admin) || typeOfAccount.equals(developer)) {
120            return logFile("eu.dasish.annotation.backend.logServerLocation", n);
121        } else {
122            this.DEVELOPER_RIGHTS_EXPECTED();
123            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
124            return "Coucou.";
125        }
126    }
127
128    //////////////////////////////////
129    @PUT
130    @Produces(MediaType.TEXT_XML)
131    @Path("/account/{principalId}/make/{account}")
132    @Transactional(readOnly = true)
133    public String updatePrincipalsAccount(@PathParam("principalId") String principalId, @PathParam("account") String account) throws IOException {
134        Number remotePrincipalID = this.getPrincipalID();
135        if (remotePrincipalID == null) {
136            return " ";
137        }
138        String typeOfAccount = dbDispatcher.getTypeOfPrincipalAccount(remotePrincipalID);
139        if (typeOfAccount.equals(admin)) {
140            try {
141                final boolean update = dbDispatcher.updateAccount(UUID.fromString(principalId), account);
142                return (update ? "The account is updated" : "The account is not updated, see the log.");
143            } catch (NotInDataBaseException e) {
144                httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
145                return e.toString();
146            }
147        } else {
148            this.ADMIN_RIGHTS_EXPECTED();
149            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
150            return "Coucou.";
151        }
152
153    }
154   
155    //////////////////////////////////
156    @PUT
157    @Produces(MediaType.TEXT_XML)
158    @Path("/resource/{resource}/{oldId: " + BackendConstants.regExpIdentifier + "}/newid/{newId:" + BackendConstants.regExpIdentifier + "}")
159    public String updateResourceIdentifier(@PathParam("resource") String resource, @PathParam("oldId") String oldExternalId, @PathParam("newId") String newExternalId) throws IOException {
160        Number remotePrincipalID = this.getPrincipalID();
161        if (remotePrincipalID == null) {
162            return "null in;ogged principal";
163        }
164        String typeOfAccount = dbDispatcher.getTypeOfPrincipalAccount(remotePrincipalID);
165        if (typeOfAccount.equals(admin)) {
166            try {
167                final boolean update = dbDispatcher.updateResourceIdentifier(Resource.valueOf(resource), UUID.fromString(oldExternalId), UUID.fromString(newExternalId));
168                return (update ? "The identifier is updated" : "The account is not updated, see the log.");
169            } catch (NotInDataBaseException e) {
170                httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
171                return e.toString();
172            }
173        } else {
174            this.ADMIN_RIGHTS_EXPECTED();
175            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
176            return "Dooooeeeii!!";
177        }
178
179    }
180
181    ///////////////////////////////////////////////////
182    private String logFile(String location, int n) throws IOException {
183        BufferedReader read = new BufferedReader(new FileReader(context.getInitParameter(location)));
184        List<String> lines = new ArrayList<String>();
185        StringBuilder result = new StringBuilder();
186        int i = 0;
187        String line;
188        while ((line = read.readLine()) != null) {
189            lines.add(line);
190            i++;
191        }
192        // want to read the last n rows, i.e. the rows (i-1), (i-1-1),...,(i-1-(n-1))
193        int last = (i > n) ? (i - n) : 0;
194        for (int j = i - 1; j >= last; j--) {
195            result.append(lines.get(j)).append("\n");
196        }
197        return result.toString();
198    }
199   
200    private void DEVELOPER_RIGHTS_EXPECTED() throws IOException {
201        loggerServer.debug("The request can be performed only by the principal with the developer's or admin rights. The logged in principal does not have either developer's or admin rights.");
202    }
203}
Note: See TracBrowser for help on using the repository browser.