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

Last change on this file since 4532 was 4532, checked in by olhsha, 10 years ago

refactoring verbose server output. Done. However needs "visual" testing.

File size: 9.1 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 java.io.IOException;
21import javax.servlet.http.HttpServletResponse;
22import org.slf4j.Logger;
23
24/**
25 *
26 * @author olhsha
27 */
28public class VerboseOutput {
29   
30    private HttpServletResponse httpServletResponse;
31    private  Logger logger;
32
33    private class MessageStatus{
34        String message;
35        int status;
36       
37        public MessageStatus(String msg, int status){
38            message = msg;
39            this.status = status;
40        }
41    }
42   
43    public VerboseOutput(HttpServletResponse httpServletResponse, Logger logger) {
44        this.httpServletResponse = httpServletResponse;
45        this.logger = logger;
46    }
47   
48   
49   
50    private MessageStatus _FORBIDDEN_NOTEBOOK_READING(String identifier) {
51        return new MessageStatus(" The logged-in principal cannot read the notebook with the identifier " + identifier, HttpServletResponse.SC_FORBIDDEN);
52    }
53   
54    private MessageStatus _FORBIDDEN_NOTEBOOK_WRITING(String identifier) {
55        return new MessageStatus(" The logged-in principal cannot write in the notebook with the identifier " + identifier, HttpServletResponse.SC_FORBIDDEN);
56    }
57   
58    private MessageStatus  _FORBIDDEN_ANNOTATION_READING(String identifier) {
59        return new MessageStatus(" The logged-in principal cannot read the annotation with the identifier " + identifier, HttpServletResponse.SC_FORBIDDEN);
60    }
61   
62    private MessageStatus  _FORBIDDEN_ANNOTATION_WRITING(String identifier) {
63        return new MessageStatus(" The logged-in principal cannot write in the annotation with the identifier " + identifier, HttpServletResponse.SC_FORBIDDEN);
64    }
65   
66    private MessageStatus  _FORBIDDEN_PERMISSION_CHANGING(String identifier) {
67        return new MessageStatus(" The logged-in principal cannot change the permission of the resource with with the identifier " + identifier+". Only the owner of the resource is allowed to chnange permissions.",HttpServletResponse.SC_FORBIDDEN);
68    }
69
70    private MessageStatus  _ILLEGAL_UUID(String identifier) {
71        return new MessageStatus("The string '" + identifier + "' is not a valid UUID.", HttpServletResponse.SC_BAD_REQUEST);
72    }
73   
74
75    private MessageStatus  _REMOTE_PRINCIPAL_NOT_FOUND(String remoteID) {
76        return new MessageStatus("The loggedinprincipal with the remote ID " + remoteID + " is not found in the database.", HttpServletResponse.SC_NOT_FOUND);
77    }
78   
79    private MessageStatus  resourceNotFound(String externalIdentifier, String resourceType) {
80        return new MessageStatus("A(n) " + resourceType + " with the indentifier " + externalIdentifier + " is not found in the database.", HttpServletResponse.SC_NOT_FOUND);
81    }
82   
83     private MessageStatus  _PRINCIPAL_NOT_FOUND(String externalIdentifier) {
84        return resourceNotFound(externalIdentifier, "principal");
85    }
86
87    private MessageStatus  _ANNOTATION_NOT_FOUND(String externalIdentifier) {
88        return resourceNotFound(externalIdentifier, "annotation");
89    }
90
91    private MessageStatus  _NOTEBOOK_NOT_FOUND(String externalIdentifier) {
92        return resourceNotFound(externalIdentifier, "notebook");
93    }
94
95    private MessageStatus  _TARGET_NOT_FOUND(String externalIdentifier) {
96        return resourceNotFound(externalIdentifier, "target");
97    }
98
99    private MessageStatus  _CACHED_REPRESENTATION_NOT_FOUND(String externalIdentifier) {
100        return resourceNotFound(externalIdentifier, "cached representation");
101    }
102
103    private MessageStatus  _INVALID_PERMISSION_MODE(String permissionMode) {
104        return new MessageStatus(permissionMode + " is an invalid permission value, which must be either owner, or reader, or writer.", HttpServletResponse.SC_BAD_REQUEST);
105    }
106   
107    private MessageStatus _IDENTIFIER_MISMATCH(String identifier){
108        return new MessageStatus("Wrong request: the annotation identifier   " + identifier + " and the annotation ID from the request body do not match.", HttpServletResponse.SC_BAD_REQUEST);
109    }
110   
111    private MessageStatus _ADMIN_RIGHTS_EXPECTED(){
112        return new MessageStatus("The request can be performed only by the principal with the admin rights. The logged in principal does not have admin rights.", HttpServletResponse.SC_FORBIDDEN);
113    }
114
115     private MessageStatus _DEVELOPER_RIGHTS_EXPECTED(){
116        return new MessageStatus("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.", HttpServletResponse.SC_FORBIDDEN);
117    }
118     
119     private MessageStatus  _PRINCIPAL_NOT_FOUND_BY_INFO(String email) {
120        return new MessageStatus("The principal with the info (e-mail) "+email+" is not found in the database.", HttpServletResponse.SC_NOT_FOUND);
121    }
122     
123    private MessageStatus  _PRINCIPAL_IS_NOT_ADDED_TO_DB() {
124        return new MessageStatus("The principal is not added the database, probably becuase another principal with the same e-mail already exists in the data base.", HttpServletResponse.SC_BAD_REQUEST);
125    } 
126   
127    private MessageStatus  _ACCOUNT_IS_NOT_UPDATED() {
128        return new MessageStatus("The account is not updated", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
129    } 
130   
131    private void sendMessage(MessageStatus msg) throws IOException {
132        logger.debug(msg.status + ": " + msg.message);
133        httpServletResponse.sendError(msg.status, msg.message);
134    }
135   
136    ///////////////////////////////
137   
138    public void FORBIDDEN_NOTEBOOK_READING(String identifier) throws IOException{
139        this.sendMessage(this._FORBIDDEN_NOTEBOOK_READING(identifier));
140    }
141   
142    public void FORBIDDEN_NOTEBOOK_WRITING(String identifier) throws IOException{
143       this.sendMessage(this._FORBIDDEN_NOTEBOOK_WRITING(identifier));
144    }
145   
146     public void  FORBIDDEN_ANNOTATION_READING(String identifier) throws IOException {
147        this.sendMessage(this._FORBIDDEN_ANNOTATION_READING(identifier));
148    }
149   
150    public void  FORBIDDEN_ANNOTATION_WRITING(String identifier) throws IOException{
151        this.sendMessage(this._FORBIDDEN_ANNOTATION_WRITING(identifier));
152    }
153   
154    public void  FORBIDDEN_PERMISSION_CHANGING(String identifier) throws IOException{
155        this.sendMessage(this._FORBIDDEN_PERMISSION_CHANGING(identifier));
156    }
157
158    public void  ILLEGAL_UUID(String identifier) throws IOException{
159        this.sendMessage(this._ILLEGAL_UUID(identifier));
160     }
161   
162
163    public void  REMOTE_PRINCIPAL_NOT_FOUND(String remoteID) throws IOException{
164        this.sendMessage(this._REMOTE_PRINCIPAL_NOT_FOUND(remoteID));
165    }
166   
167   
168     public void  PRINCIPAL_NOT_FOUND(String externalIdentifier) throws IOException {
169        this.sendMessage(this._PRINCIPAL_NOT_FOUND(externalIdentifier));
170    }
171
172    public void ANNOTATION_NOT_FOUND(String externalIdentifier) throws IOException{
173        this.sendMessage(this._ANNOTATION_NOT_FOUND(externalIdentifier));
174    }
175
176    public  void   NOTEBOOK_NOT_FOUND(String externalIdentifier) throws IOException{
177        this.sendMessage(this._NOTEBOOK_NOT_FOUND(externalIdentifier));
178     }
179
180    public  void   TARGET_NOT_FOUND(String externalIdentifier) throws IOException{
181        this.sendMessage(this._TARGET_NOT_FOUND(externalIdentifier));
182     }
183
184    public  void   CACHED_REPRESENTATION_NOT_FOUND(String externalIdentifier) throws IOException{
185        this.sendMessage(this._CACHED_REPRESENTATION_NOT_FOUND(externalIdentifier));
186    }
187
188    public  void   INVALID_PERMISSION_MODE(String permissionMode) throws IOException{
189        this.sendMessage(this._INVALID_PERMISSION_MODE(permissionMode));
190    }
191   
192    public  void  IDENTIFIER_MISMATCH(String externalIdentifier)throws IOException{
193        this.sendMessage(this._IDENTIFIER_MISMATCH(externalIdentifier));
194     }
195 
196    public  void  ADMIN_RIGHTS_EXPECTED()throws IOException{
197        this.sendMessage(this._ADMIN_RIGHTS_EXPECTED());
198     }
199   
200   public  void  DEVELOPER_RIGHTS_EXPECTED()throws IOException{
201        this.sendMessage(this._DEVELOPER_RIGHTS_EXPECTED());
202     }
203   
204     public void  PRINCIPAL_NOT_FOUND_BY_INFO(String email) throws IOException {
205        this.sendMessage(this._PRINCIPAL_NOT_FOUND_BY_INFO(email));
206    }
207     
208    public void   PRINCIPAL_IS_NOT_ADDED_TO_DB() throws IOException {
209        this.sendMessage(this._PRINCIPAL_IS_NOT_ADDED_TO_DB());
210    }
211   
212     public void   ACCOUNT_IS_NOT_UPDATED() throws IOException {
213        this.sendMessage(this._ACCOUNT_IS_NOT_UPDATED());
214    }
215}
Note: See TracBrowser for help on using the repository browser.