Changeset 4532


Ignore:
Timestamp:
02/18/14 15:12:30 (10 years ago)
Author:
olhsha
Message:

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

Location:
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java

    r4529 r4532  
    122122                        return new ObjectFactory().createAnnotation(annotation);
    123123                    } else {
    124                         verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_ANNOTATION_READING(externalIdentifier), HttpServletResponse.SC_FORBIDDEN);
    125                     }
    126                 } else {
    127                     verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
    128 
    129                 }
    130             } else {
    131                 verboseOutput.sendFailureMessage(VerboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier), HttpServletResponse.SC_NOT_FOUND);
    132             }
    133         } catch (IllegalArgumentException e) {
    134             verboseOutput.sendFailureMessage(VerboseOutput.ILLEGAL_UUID(externalIdentifier), HttpServletResponse.SC_BAD_REQUEST);
     124                        verboseOutput.FORBIDDEN_ANNOTATION_READING(externalIdentifier);
     125                    }
     126                } else {
     127                    verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     128                }
     129            } else {
     130                verboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier);
     131            }
     132        } catch (IllegalArgumentException e) {
     133            verboseOutput.ILLEGAL_UUID(externalIdentifier);
    135134        }
    136135        return new ObjectFactory().createAnnotation(new Annotation());
     
    154153                        return new ObjectFactory().createTargetList(TargetList);
    155154                    } else {
    156                         verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_ANNOTATION_READING(externalIdentifier), HttpServletResponse.SC_FORBIDDEN);
    157                     }
    158                 } else {
    159                     verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
    160 
    161                 }
    162             } else {
    163                 verboseOutput.sendFailureMessage(VerboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier), HttpServletResponse.SC_NOT_FOUND);
    164 
    165             }
    166         } catch (IllegalArgumentException e) {
    167             verboseOutput.sendFailureMessage(VerboseOutput.ILLEGAL_UUID(externalIdentifier), HttpServletResponse.SC_BAD_REQUEST);
     155                        verboseOutput.FORBIDDEN_ANNOTATION_READING(externalIdentifier);
     156                    }
     157                } else {
     158                    verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     159
     160                }
     161            } else {
     162                verboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier);
     163
     164            }
     165        } catch (IllegalArgumentException e) {
     166            verboseOutput.ILLEGAL_UUID(externalIdentifier);
    168167        }
    169168        return new ObjectFactory().createTargetList(new ReferenceList());
     
    193192                return new ObjectFactory().createAnnotationInfoList(annotationInfoList);
    194193            } catch (IllegalArgumentException e) {
    195                 verboseOutput.sendFailureMessage(VerboseOutput.ILLEGAL_UUID(ownerExternalId), HttpServletResponse.SC_BAD_REQUEST);
     194                verboseOutput.ILLEGAL_UUID(ownerExternalId);
    196195            }
    197196        } else {
    198             verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
     197            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    199198
    200199        }
     
    219218                        return new ObjectFactory().createPermissionList(permissionList);
    220219                    } else {
    221                         verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_ANNOTATION_READING(externalIdentifier), HttpServletResponse.SC_FORBIDDEN);
    222                     }
    223                 } else {
    224                     verboseOutput.sendFailureMessage(VerboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier), HttpServletResponse.SC_NOT_FOUND);
    225                 }
    226             } else {
    227                 verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
    228 
    229             }
    230         } catch (IllegalArgumentException e) {
    231             verboseOutput.sendFailureMessage(VerboseOutput.ILLEGAL_UUID(externalIdentifier), HttpServletResponse.SC_BAD_REQUEST);
     220                        verboseOutput.FORBIDDEN_ANNOTATION_READING(externalIdentifier);
     221                    }
     222                } else {
     223                    verboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier);
     224                }
     225            } else {
     226                verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     227
     228            }
     229        } catch (IllegalArgumentException e) {
     230            verboseOutput.ILLEGAL_UUID(externalIdentifier);
    232231        }
    233232        return new ObjectFactory().createUserWithPermissionList(new UserWithPermissionList());
     
    251250                        return result + " annotation(s) deleted.";
    252251                    } else {
    253                         verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_ANNOTATION_WRITING(externalIdentifier) + " Only a principal with 'owner' access can delete the annotation.", HttpServletResponse.SC_FORBIDDEN);
    254 
    255                     }
    256                 } else {
    257                     verboseOutput.sendFailureMessage(VerboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier), HttpServletResponse.SC_NOT_FOUND);
    258                 }
    259 
    260             } else {
    261                 verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
    262 
    263             }
    264         } catch (IllegalArgumentException e) {
    265             verboseOutput.sendFailureMessage(VerboseOutput.ILLEGAL_UUID(externalIdentifier), HttpServletResponse.SC_BAD_REQUEST);
     252                        verboseOutput.FORBIDDEN_ANNOTATION_WRITING(externalIdentifier);
     253
     254                    }
     255                } else {
     256                    verboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier);
     257                }
     258
     259            } else {
     260                verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     261
     262            }
     263        } catch (IllegalArgumentException e) {
     264            verboseOutput.ILLEGAL_UUID(externalIdentifier);
    266265        }
    267266
     
    283282            return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID));
    284283        } else {
    285             verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
     284            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    286285
    287286        }
     
    301300
    302301        if (!(path + "annotations/" + externalIdentifier).equals(annotationURI)) {
    303             verboseOutput.sendFailureMessage("Wrong request: the annotation identifier   " + externalIdentifier + " and the annotation ID from the request body do not match. Correct the request and resend.", HttpServletResponse.SC_CONFLICT);
     302            verboseOutput.IDENTIFIER_MISMATCH(externalIdentifier);
    304303            return new ObjectFactory().createResponseBody(new ResponseBody());
    305304        }
     
    316315                        return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID));
    317316                    } else {
    318                         verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_PERMISSION_CHANGING(externalIdentifier) + " Permission changing is the part of the full update of the annotation.", HttpServletResponse.SC_FORBIDDEN);
    319                     }
    320                 } else {
    321                     verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
    322                 }
    323             } else {
    324                 verboseOutput.sendFailureMessage(VerboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier), HttpServletResponse.SC_NOT_FOUND);
    325             }
    326         } catch (IllegalArgumentException e) {
    327             verboseOutput.sendFailureMessage(VerboseOutput.ILLEGAL_UUID(externalIdentifier), HttpServletResponse.SC_BAD_REQUEST);
     317                        verboseOutput.FORBIDDEN_PERMISSION_CHANGING(externalIdentifier);
     318                        loggerServer.debug(" Permission changing is the part of the full update of the annotation.");
     319                    }
     320                } else {
     321                    verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     322                }
     323            } else {
     324                verboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier);
     325            }
     326        } catch (IllegalArgumentException e) {
     327            verboseOutput.ILLEGAL_UUID(externalIdentifier);
    328328        }
    329329        return new ObjectFactory().createResponseBody(new ResponseBody());
     
    347347                        return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID));
    348348                    } else {
    349                         verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_ANNOTATION_WRITING(externalIdentifier), HttpServletResponse.SC_FORBIDDEN);
    350                     }
    351                 } else {
    352                     verboseOutput.sendFailureMessage(VerboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier), HttpServletResponse.SC_NOT_FOUND);
    353                 }
    354             } else {
    355                 verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
    356 
    357             }
    358         } catch (IllegalArgumentException e) {
    359             verboseOutput.sendFailureMessage(VerboseOutput.ILLEGAL_UUID(externalIdentifier), HttpServletResponse.SC_BAD_REQUEST);
     349                        verboseOutput.FORBIDDEN_ANNOTATION_WRITING(externalIdentifier);
     350                    }
     351                } else {
     352                    verboseOutput.ANNOTATION_NOT_FOUND(externalIdentifier);
     353                }
     354            } else {
     355                verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     356
     357            }
     358        } catch (IllegalArgumentException e) {
     359            verboseOutput.ILLEGAL_UUID(externalIdentifier);
    360360        }
    361361        return new ObjectFactory().createResponseBody(new ResponseBody());
     
    385385
    386386                            } else {
    387                                 verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_PERMISSION_CHANGING(annotationExternalId), HttpServletResponse.SC_FORBIDDEN);
     387                                verboseOutput.FORBIDDEN_PERMISSION_CHANGING(annotationExternalId);
    388388                            }
    389389                        } else {
    390                             verboseOutput.sendFailureMessage(VerboseOutput.ANNOTATION_NOT_FOUND(annotationExternalId), HttpServletResponse.SC_NOT_FOUND);
     390                            verboseOutput.ANNOTATION_NOT_FOUND(annotationExternalId);
    391391                        }
    392392                    } catch (IllegalArgumentException e) {
    393                         verboseOutput.sendFailureMessage(VerboseOutput.ILLEGAL_UUID(annotationExternalId), HttpServletResponse.SC_BAD_REQUEST);
    394                     }
    395                 } else {
    396                     verboseOutput.sendFailureMessage(VerboseOutput.PRINCIPAL_NOT_FOUND(userExternalId), HttpServletResponse.SC_NOT_FOUND);
     393                        verboseOutput.ILLEGAL_UUID(annotationExternalId);
     394                    }
     395                } else {
     396                    verboseOutput.PRINCIPAL_NOT_FOUND(userExternalId);
    397397                }
    398398            } catch (IllegalArgumentException e) {
    399                 verboseOutput.sendFailureMessage(VerboseOutput.ILLEGAL_UUID(userExternalId), HttpServletResponse.SC_BAD_REQUEST);
     399                verboseOutput.ILLEGAL_UUID(userExternalId);
    400400            }
    401401
    402402        } else {
    403             verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
     403            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    404404
    405405        }
     
    423423                        return new ObjectFactory().createResponseBody(makePermissionResponseEnvelope(annotationID));
    424424                    } else {
    425                         verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_PERMISSION_CHANGING(annotationExternalId), HttpServletResponse.SC_FORBIDDEN);
    426                     }
    427                 } else {
    428                     verboseOutput.sendFailureMessage(VerboseOutput.ANNOTATION_NOT_FOUND(annotationExternalId), HttpServletResponse.SC_NOT_FOUND);
    429                 }
    430             } else {
    431                 verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
     425                        verboseOutput.FORBIDDEN_PERMISSION_CHANGING(annotationExternalId);
     426                    }
     427                } else {
     428                    verboseOutput.ANNOTATION_NOT_FOUND(annotationExternalId);
     429                }
     430            } else {
     431                verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    432432
    433433            }
     
    435435
    436436        } catch (IllegalArgumentException e) {
    437             verboseOutput.sendFailureMessage(VerboseOutput.ILLEGAL_UUID(annotationExternalId), HttpServletResponse.SC_BAD_REQUEST);
     437            verboseOutput.ILLEGAL_UUID(annotationExternalId);
    438438            return new ObjectFactory().createResponseBody(new ResponseBody());
    439439
     
    459459
    460460                        } else {
    461                             verboseOutput.sendFailureMessage(VerboseOutput.PRINCIPAL_NOT_FOUND(userId), HttpServletResponse.SC_NOT_FOUND);
     461                            verboseOutput.PRINCIPAL_NOT_FOUND(userId);
    462462                        }
    463463                    } else {
    464                         verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_PERMISSION_CHANGING(annotationId), HttpServletResponse.SC_FORBIDDEN);
    465 
    466                     }
    467                 } else {
    468                     verboseOutput.sendFailureMessage(VerboseOutput.ANNOTATION_NOT_FOUND(annotationId), HttpServletResponse.SC_NOT_FOUND);
    469                 }
    470             } else {
    471                 verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
    472 
    473             }
    474         } catch (IllegalArgumentException e) {
    475             verboseOutput.sendFailureMessage(VerboseOutput.ILLEGAL_UUID(annotationId), HttpServletResponse.SC_BAD_REQUEST);
     464                        verboseOutput.FORBIDDEN_PERMISSION_CHANGING(annotationId);
     465
     466                    }
     467                } else {
     468                    verboseOutput.ANNOTATION_NOT_FOUND(annotationId);
     469                }
     470            } else {
     471                verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     472
     473            }
     474        } catch (IllegalArgumentException e) {
     475            verboseOutput.ILLEGAL_UUID(annotationId);
    476476        }
    477477        return (deletedRows + " is deleted.");
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/CachedRepresentationResource.java

    r4301 r4532  
    6262    @Context
    6363    private UriInfo uriInfo;
    64     private final Logger logger = LoggerFactory.getLogger(CachedRepresentationResource.class);
    65 
     64    public static final Logger loggerServer = LoggerFactory.getLogger(HttpServletResponse.class);
     65    private final VerboseOutput verboseOutput = new VerboseOutput(httpServletResponse, loggerServer);
     66   
     67   
    6668    public void setHttpRequest(HttpServletRequest request) {
    6769        this.httpServletRequest = request;
     
    8688                    return new ObjectFactory().createCashedRepresentationInfo(cachedInfo);
    8789                } else {
    88                     AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": The cached representation with the given id is not found in the database");
    89                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation with the given id is not found in the database");
    90                     return null;
     90                    verboseOutput.CACHED_REPRESENTATION_NOT_FOUND(externalId);
    9191                }
    9292            } catch (IllegalArgumentException e) {
    93                 AnnotationResource.loggerServer.debug(HttpServletResponse.SC_BAD_REQUEST + ": Illegal argument UUID " + externalId);
    94                 httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument UUID " + externalId);
    95                 return null;
     93                 verboseOutput.ILLEGAL_UUID(externalId);
    9694            }
    9795        } else {
    98             AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": the logged-in user is not found in the database");
    99             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged in user is not found in the database");
    100             return null;
     96            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    10197        }
     98       
     99        return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
    102100    }
    103101
     
    119117                    return result;
    120118                } else {
    121                     AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + "The cached representation with the given id is not found in the database");
    122                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation  with the given id   " + externalId + " is not found in the database");
    123                     return null;
     119                    verboseOutput.ANNOTATION_NOT_FOUND(externalId);
    124120                }
    125121            } catch (IllegalArgumentException e) {
    126                 AnnotationResource.loggerServer.debug(HttpServletResponse.SC_BAD_REQUEST + ": Illegal argument UUID " + externalId);
    127                 httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument UUID " + externalId);
    128                 return null;
     122                 verboseOutput.ILLEGAL_UUID(externalId);
    129123            }
    130124        } else {
    131             AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": the logged-in user is not found in the database");
    132             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    133             return null;
     125            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    134126        }
     127       
     128         return null;
    135129    }
    136130}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/DebugResource.java

    r4281 r4532  
    5252@Path("/debug")
    5353public class DebugResource {
    54    @Autowired
     54
     55    @Autowired
    5556    private DBIntegrityService dbIntegrityService;
    5657    @Context
     
    6061    @Context
    6162    private UriInfo uriInfo;
    62    
    6363    @Context
    6464    private ServletContext context;
    65    
    6665    final String default_permission = "reader";
    67     private final Logger logger = LoggerFactory.getLogger(DebugResource.class);
     66    public static final Logger loggerServer = LoggerFactory.getLogger(HttpServletResponse.class);
     67    private final VerboseOutput verboseOutput = new VerboseOutput(httpServletResponse, loggerServer);
    6868    private final String admin = "admin";
    69     private final String developer = "developer"; 
    70    
     69    private final String developer = "developer";
     70
    7171    @GET
    7272    @Produces(MediaType.TEXT_XML)
     
    8383                return new ObjectFactory().createAnnotationInfoList(annotationInfoList);
    8484            } else {
    85                 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "The logged-in user is neither developer nor admin, and therefore cannot perform this request.");
    86                 return null;
     85                verboseOutput.DEVELOPER_RIGHTS_EXPECTED();
    8786            }
    8887        } else {
    89             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    90             return null;
     88            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    9189        }
    92 
     90        return new ObjectFactory().createAnnotationInfoList(new AnnotationInfoList());
    9391    }
    9492
     
    9896    @Transactional(readOnly = true)
    9997    public String getDasishBackendLog(@PathParam("n") int n) throws IOException {
    100         return logFile("eu.dasish.annotation.backend.logDatabaseLocation", n);
     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 " ";
    101112    }
    102    
     113
    103114    @GET
    104115    @Produces(MediaType.TEXT_PLAIN)
     
    108119        return httpServletRequest.getRemoteUser();
    109120    }
    110    
     121
    111122    /////
    112    
    113123    @GET
    114124    @Produces(MediaType.TEXT_PLAIN)
     
    116126    @Transactional(readOnly = true)
    117127    public String getDasishServerLog(@PathParam("n") int n) throws IOException {
    118         return logFile("eu.dasish.annotation.backend.logServerLocation", n);
     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
    119142    }
    120    
     143
    121144    //////////////////////////////////
    122145    @PUT
     
    134157                return (update ? "The account is updated" : "The account is not updated, see the log.");
    135158            } else {
    136                 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "The logged-in user is not admin, and therefore cannot perform this request.");
    137                 return null;
     159                verboseOutput.ADMIN_RIGHTS_EXPECTED();
    138160            }
    139161        } else {
    140             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    141             return null;
     162            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    142163        }
    143 
     164        return " ";
    144165    }
    145166
    146167    ///////////////////////////////////////////////////
    147     private String logFile(String location, int n) throws IOException{
    148        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    149         String remoteUser = httpServletRequest.getRemoteUser();
    150         Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(remoteUser);
    151         if (userID != null) {
    152             String typeOfAccount = dbIntegrityService.getTypeOfUserAccount(userID);
    153             if (typeOfAccount.equals(admin) || typeOfAccount.equals(developer)) {
    154                 BufferedReader reader = new BufferedReader(new FileReader(context.getInitParameter(location)));
    155                 List<String> lines = new ArrayList<String>();
    156                 StringBuilder result = new StringBuilder();
    157                 int i = 0;
    158                 String line;
    159                 while ((line = reader.readLine()) != null) {
    160                     lines.add(line);
    161                     i++;
    162                 }
    163                 // want to read the last n rows, i.e. the rows (i-1), (i-1-1),...,(i-1-(n-1))
    164                 int last = (i > n) ? (i - n) : 0;
    165                 for (int j = i - 1; j >= last; j--) {
    166                     result.append(lines.get(j)).append("\n");
    167                 }
    168                 return result.toString();
    169 
    170             } else {
    171                 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "The logged-in user is neither developer nor admin, and therefore cannot perform this request.");
    172                 return null;
    173             }
    174         } else {
    175             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    176             return null;
    177         }
     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();
    178184    }
    179185}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/NotebookResource.java

    r4529 r4532  
    8888                return new ObjectFactory().createNotebookInfoList(notebookInfos);
    8989            } else {
    90                 verboseOutput.sendFailureMessage(VerboseOutput.INVALID_PERMISSION_MODE(permissionMode), httpServletResponse.SC_BAD_REQUEST);
    91             }
    92         } else {
    93             verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
     90                verboseOutput.INVALID_PERMISSION_MODE(permissionMode);
     91            }
     92        } else {
     93            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    9494        }
    9595        return (new ObjectFactory()).createNotebookInfoList(new NotebookInfoList());
     
    110110            return new ObjectFactory().createReferenceList(references);
    111111        } else {
    112             verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, httpServletResponse.SC_NOT_FOUND);
     112            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    113113        }
    114114        return new ObjectFactory().createReferenceList(new ReferenceList());
     
    132132                    return new ObjectFactory().createReferenceList(principals);
    133133                } else {
    134                     verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_NOTEBOOK_READING(externalIdentifier), HttpServletResponse.SC_FORBIDDEN);
     134                    verboseOutput.FORBIDDEN_NOTEBOOK_READING(externalIdentifier);
    135135                }
    136136            } else {
    137                 verboseOutput.sendFailureMessage(VerboseOutput.NOTEBOOK_NOT_FOUND(externalIdentifier), HttpServletResponse.SC_NOT_FOUND);
    138             }
    139         } else {
    140             verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, HttpServletResponse.SC_NOT_FOUND);
     137                verboseOutput.NOTEBOOK_NOT_FOUND(externalIdentifier);
     138            }
     139        } else {
     140            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    141141        }
    142142
     
    164164                    return new ObjectFactory().createNotebook(notebook);
    165165                } else {
    166                     verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_NOTEBOOK_READING(externalIdentifier), HttpServletResponse.SC_FORBIDDEN);
     166                    verboseOutput.FORBIDDEN_NOTEBOOK_READING(externalIdentifier);
    167167                }
    168168            } else {
    169                 verboseOutput.sendFailureMessage(VerboseOutput.NOTEBOOK_NOT_FOUND(externalIdentifier), HttpServletResponse.SC_NOT_FOUND);
    170             }
    171         } else {
    172             verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, HttpServletResponse.SC_NOT_FOUND);
     169                verboseOutput.NOTEBOOK_NOT_FOUND(externalIdentifier);
     170            }
     171        } else {
     172            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    173173        }
    174174        return new ObjectFactory().createNotebook(new Notebook());
     
    196196                    return new ObjectFactory().createReferenceList(annotations);
    197197                } else {
    198                     verboseOutput.sendFailureMessage(VerboseOutput.FORBIDDEN_NOTEBOOK_READING(externalIdentifier), HttpServletResponse.SC_FORBIDDEN);
     198                    verboseOutput.FORBIDDEN_NOTEBOOK_READING(externalIdentifier);
    199199                }
    200200            } else {
    201                 verboseOutput.sendFailureMessage(VerboseOutput.NOTEBOOK_NOT_FOUND(externalIdentifier), HttpServletResponse.SC_NOT_FOUND);
    202             }
    203         } else {
    204             verboseOutput.sendFailureMessage(VerboseOutput.REMOTE_PRINCIPAL_NOT_FOUND, HttpServletResponse.SC_NOT_FOUND);
     201                verboseOutput.NOTEBOOK_NOT_FOUND(externalIdentifier);
     202            }
     203        } else {
     204            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
    205205        }
    206206        return new ObjectFactory().createReferenceList(new ReferenceList());
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/TargetResource.java

    r4301 r4532  
    7171    @Context
    7272    private UriInfo uriInfo;
    73     private final Logger logger = LoggerFactory.getLogger(TargetResource.class);
    74 
     73    public static final Logger loggerServer = LoggerFactory.getLogger(HttpServletResponse.class);
     74    private final VerboseOutput verboseOutput = new VerboseOutput(httpServletResponse, loggerServer);
    7575    public void setHttpRequest(HttpServletRequest request) {
    7676        this.httpServletRequest = request;
     
    9696                    return new ObjectFactory().createTarget(target);
    9797                } else {
    98                     AnnotationResource.loggerServer.debug(HttpServletResponse.SC_NOT_FOUND + ": The target with the given id " + externalIdentifier + " is not found in the database");
    99                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The target with the given id   " + externalIdentifier + " is not found in the database");
    100                     return null;
    101                 }
    102             } catch (IllegalArgumentException e) {
    103                 AnnotationResource.loggerServer.debug(HttpServletResponse.SC_BAD_REQUEST + ": Illegal argument UUID " + externalIdentifier);
    104                 httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument UUID " + externalIdentifier);
    105                 return null;
    106             }
    107         } else {
    108             AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": the logged-in user is not found in the database");
    109             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    110             return null;
    111         }
     98                   verboseOutput.TARGET_NOT_FOUND(externalIdentifier);
     99                }
     100            } catch (IllegalArgumentException e) {
     101                verboseOutput.ILLEGAL_UUID(externalIdentifier);
     102            }
     103        } else {
     104           verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     105        }
     106        return new ObjectFactory().createTarget(new Target());
    112107    }
    113108
     
    128123                    return new ObjectFactory().createReferenceList(siblings);
    129124                } else {
    130                     AnnotationResource.loggerServer.debug(HttpServletResponse.SC_NOT_FOUND + ": The target with the given id " + externalIdentifier + " is not found in the database");
    131                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The target with the given id   " + externalIdentifier + " is not found in the database");
    132                     return null;
    133                 }
    134             } catch (IllegalArgumentException e) {
    135                 AnnotationResource.loggerServer.debug(HttpServletResponse.SC_BAD_REQUEST + ": Illegal argument UUID " + externalIdentifier);
    136                 httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument UUID " + externalIdentifier);
    137                 return null;
    138             }
    139         } else {
    140             AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": the logged-in user is not found in the database");
    141             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    142             return null;
    143         }
    144     }
    145 
    146 // TODO both unit tests
    147 //changed path, /Targetpart is removed
    148 //how to overwork the input stream to make it downloadable
    149 // using mime type as well
    150 //    @DELETE
    151 //    @Produces(MediaType.TEXT_XML)
    152 //    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/cached/{cachedid: " + BackendConstants.regExpIdentifier + "}")
    153 //    @Secured("ROLE_ADMIN")
    154 //    public int deleteCached(@PathParam("targetid") String targetIdentifier, @PathParam("cachedid") String cachedIdentifier) throws SQLException {
    155 //        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    156 //        final Number targetID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(targetIdentifier));
    157 //        final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(cachedIdentifier));
    158 //        int[] result = dbIntegrityService.deleteCachedRepresentationOfTarget(targetID, cachedID);
    159 //        return result[1];
    160 //    }
     125                    verboseOutput.TARGET_NOT_FOUND(externalIdentifier);
     126                }
     127            } catch (IllegalArgumentException e) {
     128                verboseOutput.ILLEGAL_UUID(externalIdentifier);
     129            }
     130        } else {
     131            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     132        }
     133       
     134        return new ObjectFactory().createReferenceList(new ReferenceList());
     135    }
     136
     137
    161138    @POST
    162139    @Consumes("multipart/mixed")
     
    178155                    final Number[] respondDB = dbIntegrityService.addCachedForTarget(targetID, fragmentDescriptor, metadata, cachedSource);
    179156                    final CachedRepresentationInfo cachedInfo = dbIntegrityService.getCachedRepresentationInfo(respondDB[1]);
    180                     return new ObjectFactory()
    181                             .createCashedRepresentationInfo(cachedInfo);
    182                 } else {
    183                     AnnotationResource.loggerServer.debug(HttpServletResponse.SC_NOT_FOUND + ": The target with the given id " + targetIdentifier + " is not found in the database");
    184                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The target with the given id   " + targetIdentifier + " is not found in the database");
    185                     return null;
    186                 }
    187             } catch (IllegalArgumentException e) {
    188                 AnnotationResource.loggerServer.debug(HttpServletResponse.SC_BAD_REQUEST + ": Illegal argument UUID " + targetIdentifier);
    189                 httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument UUID " + targetIdentifier);
    190                 return null;
    191             }
    192         } else {
    193             AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": the logged-in user is not found in the database");
    194             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    195             return null;
    196         }
    197 
     157                    return new ObjectFactory().createCashedRepresentationInfo(cachedInfo);
     158                } else {
     159                    verboseOutput.TARGET_NOT_FOUND(targetIdentifier);
     160                }
     161            } catch (IllegalArgumentException e) {
     162                verboseOutput.ILLEGAL_UUID(targetIdentifier);
     163            }
     164        } else {
     165           verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     166        }
     167       return new ObjectFactory().createCashedRepresentationInfo(new CachedRepresentationInfo());
    198168    }
    199169
     
    215185                        return result + " pair(s) target-cached deleted.";
    216186                    } else {
    217                         AnnotationResource.loggerServer.debug(HttpServletResponse.SC_NOT_FOUND + ": The target with the given id " + cachedExternalIdentifier + " is not found in the database");
    218                         httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The cached representation with the given id   " + cachedExternalIdentifier + " is not found in the database");
    219                         return null;
     187                        verboseOutput.CACHED_REPRESENTATION_NOT_FOUND(cachedExternalIdentifier);
    220188                    }
    221189                } else {
    222                     AnnotationResource.loggerServer.debug(HttpServletResponse.SC_NOT_FOUND + ": The target with the given id " + cachedExternalIdentifier + " is not found in the database");
    223                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The target with the given id   " + targetExternalIdentifier + " is not found in the database");
    224                     return null;
    225                 }
    226             } catch (IllegalArgumentException e) {
    227                 AnnotationResource.loggerServer.debug(HttpServletResponse.SC_BAD_REQUEST + ": Illegal argument UUID " + targetExternalIdentifier);
    228                 httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument UUID " + targetExternalIdentifier);
    229                 return null;
    230             }
    231         } else {
    232             AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": the logged-in user is not found in the database");
    233             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged in user is not found in the database");
    234             return null;
    235         }
     190                    verboseOutput.TARGET_NOT_FOUND(targetExternalIdentifier);
     191                }
     192            } catch (IllegalArgumentException e) {
     193                verboseOutput.ILLEGAL_UUID(targetExternalIdentifier);
     194            }
     195        } else {
     196            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     197        }
     198        return " ";
    236199    }
    237200}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/UserResource.java

    r4351 r4532  
    6565    @Context
    6666    private UriInfo uriInfo;
    67     private final Logger logger = LoggerFactory.getLogger(UserResource.class);
     67    public static final Logger loggerServer = LoggerFactory.getLogger(HttpServletResponse.class);
     68    private final VerboseOutput verboseOutput = new VerboseOutput(httpServletResponse, loggerServer);
     69   
    6870    final private String admin = "admin";
    6971
     
    9092                    return new ObjectFactory().createUser(user);
    9193                } else {
    92                     AnnotationResource.loggerServer.debug(HttpServletResponse.SC_NOT_FOUND + ": The user with the given id " + externalIdentifier + " is not found in the database");
    93                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id   " + externalIdentifier + " is not found in the database");
    94                     return null;
     94                    verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier);
    9595                }
    9696            } catch (IllegalArgumentException e) {
    97                 AnnotationResource.loggerServer.debug(HttpServletResponse.SC_BAD_REQUEST + ": Illegal argument UUID " + externalIdentifier);
    98                 httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument UUID " + externalIdentifier);
    99                 return null;
    100             }
    101         } else {
    102             AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": the logged-in user is not found in the database");
    103             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    104             return null;
    105         }
     97                verboseOutput.ILLEGAL_UUID(externalIdentifier);
     98            }
     99        } else {
     100            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     101        }
     102        return new ObjectFactory().createUser(new User());
    106103    }
    107104
     
    119116                return new ObjectFactory().createUser(user);
    120117            } else {
    121                 AnnotationResource.loggerServer.debug(HttpServletResponse.SC_NOT_FOUND + ": The user with the given info is not found in the database");
    122                 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given info is not found in the database");
    123                 return null;
    124             }
    125         } else {
    126             AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": the logged-in user is not found in the database");
    127             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    128             return null;
    129         }
     118                verboseOutput.PRINCIPAL_NOT_FOUND_BY_INFO(email);
     119            }
     120        } else {
     121            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     122        }
     123        return new ObjectFactory().createUser(new User());
    130124    }
    131125
     
    147141                    return new ObjectFactory().createCurrentUserInfo(userInfo);
    148142                } else {
    149                     AnnotationResource.loggerServer.debug(HttpServletResponse.SC_NOT_FOUND + ": The user with the given id " + externalIdentifier + " is not found in the database");
    150                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id   " + externalIdentifier + " is not found in the database");
    151                     return null;
     143                    verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier);
    152144                }
    153145            } catch (IllegalArgumentException e) {
    154                 AnnotationResource.loggerServer.debug(HttpServletResponse.SC_BAD_REQUEST + ": Illegal argument UUID " + externalIdentifier);
    155                 httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument UUID " + externalIdentifier);
    156                 return null;
    157             }
    158         } else {
    159             AnnotationResource.loggerServer.debug(httpServletResponse.SC_NOT_FOUND + ": the logged-in user is not found in the database");
    160             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    161             return null;
    162         }
     146                verboseOutput.ILLEGAL_UUID(externalIdentifier);
     147            }
     148        } else {
     149            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     150        }
     151       
     152         return new ObjectFactory().createCurrentUserInfo(new CurrentUserInfo());
    163153    }
    164154
     
    178168                    return new ObjectFactory().createUser(addedUser);
    179169                } else {
    180                     httpServletResponse.sendError(HttpServletResponse.SC_CONFLICT, "The user canot be added to the database, (possibly) because a user with the given e-mail already exist in the database.");
    181                     return null;
    182                 }
    183             } else {
    184                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "The logged-in user does not have admin rights to add a user to the database");
    185                 return null;
    186             }
    187         } else {
    188             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    189             return null;
    190         }
     170                     verboseOutput.PRINCIPAL_IS_NOT_ADDED_TO_DB();
     171                }
     172            } else {
     173                verboseOutput.ADMIN_RIGHTS_EXPECTED();
     174            }
     175        } else {
     176            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     177        }
     178         return new ObjectFactory().createUser(new User());
    191179    }
    192180
     
    206194                    return new ObjectFactory().createUser(addedUser);
    207195                } else {
    208                     AnnotationResource.loggerServer.debug(HttpServletResponse.SC_NOT_FOUND + ": The user with the given id is not found in the database");
    209                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id  is not found in the database");
    210                     return null;
    211                 }
    212             } else {
    213                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "The logged-in user does not have admin rights to update a user info in the database");
    214                 return null;
    215             }
    216         } else {
    217             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    218             return null;
    219         }
     196                    verboseOutput.PRINCIPAL_NOT_FOUND(user.getURI());
     197                }
     198            } else {
     199                verboseOutput.ADMIN_RIGHTS_EXPECTED();
     200            }
     201        } else {
     202            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     203        }
     204       
     205         return new ObjectFactory().createUser(new User());
    220206    }
    221207   
     
    233219                    return "The account was updated to "+dbIntegrityService.getTypeOfUserAccount(dbIntegrityService.getUserInternalIdentifier(UUID.fromString(externalId)));
    234220                } else {
    235                     httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "The account was not updated.");
    236                     return "The account was not updated.";
    237                 }
    238             } else {
    239                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "The logged-in user does not have admin rights to update an account type in the database");
    240                 return null;
    241             }
    242         } else {
    243             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    244             return null;
    245         }
     221                   verboseOutput.ACCOUNT_IS_NOT_UPDATED();
     222                }
     223            } else {
     224                verboseOutput.ADMIN_RIGHTS_EXPECTED();
     225            }
     226        } else {
     227            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     228        }
     229       
     230        return " ";
    246231    }
    247232
     
    259244                    return "There is " + result.toString() + " row deleted";
    260245                } else {
    261                     AnnotationResource.loggerServer.debug(HttpServletResponse.SC_NOT_FOUND + ": The user with the given id " + externalIdentifier + " is not found in the database");
    262                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id   " + externalIdentifier + " is not found in the database");
    263                     return null;
    264                 }
    265             } else {
    266                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "The logged-in user does not have admin rights to update a user info in the database");
    267                 return null;
    268             }
    269         } else {
    270             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    271             return null;
    272         }
     246                    verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier);
     247                }
     248            } else {
     249                verboseOutput.ADMIN_RIGHTS_EXPECTED();
     250            }
     251        } else {
     252            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     253        }
     254       
     255        return " ";
    273256    }
    274257
     
    286269                    return "There is " + result.toString() + " row deleted";
    287270                } else {
    288                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id   " + externalIdentifier + " is not found in the database");
    289                     return null;
    290                 }
    291             } else {
    292                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "The logged-in user does not have admin rights to update a user info in the database");
    293                 return null;
    294             }
    295         } else {
    296             httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged-in user is not found in the database");
    297             return null;
    298         }
     271                    verboseOutput.PRINCIPAL_NOT_FOUND(externalIdentifier);
     272                }
     273            } else {
     274                verboseOutput.ADMIN_RIGHTS_EXPECTED();
     275            }
     276        } else {
     277            verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remoteUser);
     278        }
     279        return " ";
    299280    }
    300281
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/VerboseOutput.java

    r4529 r4532  
    3131    private  Logger logger;
    3232
    33     final static public String REMOTE_PRINCIPAL_NOT_FOUND = "The logged-in principal is not found in the database";
     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    }
    3442   
    3543    public VerboseOutput(HttpServletResponse httpServletResponse, Logger logger) {
     
    3846    }
    3947   
    40     static public String FORBIDDEN_NOTEBOOK_READING(String identifier) {
    41         return " The logged-in principal cannot read the notebook with the identifier " + identifier;
    42     }
    43    
    44     static public String FORBIDDEN_NOTEBOOK_WRITING(String identifier) {
    45         return " The logged-in principal cannot write in the notebook with the identifier " + identifier;
    46     }
    47    
    48      static public String FORBIDDEN_ANNOTATION_READING(String identifier) {
    49         return " The logged-in principal cannot read the annotation with the identifier " + identifier;
    50     }
    51    
    52     static public String FORBIDDEN_ANNOTATION_WRITING(String identifier) {
    53         return " The logged-in principal cannot write in the annotation with the identifier " + identifier;
    54     }
    55    
    56      static public String FORBIDDEN_PERMISSION_CHANGING(String identifier) {
    57         return " 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.";
    58     }
    59 
    60     static public String ILLEGAL_UUID(String identifier) {
    61         return ("The string '" + identifier + "' is not a valid UUID.");
    62     }
    63 
    64     static private String resourceNotFound(String externalIdentifier, String resourceType) {
    65         return ("A(n) " + resourceType + " with the indentifier " + externalIdentifier + " is not found in the database.");
    66     }
    67 
    68     static public String PRINCIPAL_NOT_FOUND(String externalIdentifier) {
     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) {
    6984        return resourceNotFound(externalIdentifier, "principal");
    7085    }
    7186
    72     static public String ANNOTATION_NOT_FOUND(String externalIdentifier) {
     87    private MessageStatus  _ANNOTATION_NOT_FOUND(String externalIdentifier) {
    7388        return resourceNotFound(externalIdentifier, "annotation");
    7489    }
    7590
    76     static public String NOTEBOOK_NOT_FOUND(String externalIdentifier) {
     91    private MessageStatus  _NOTEBOOK_NOT_FOUND(String externalIdentifier) {
    7792        return resourceNotFound(externalIdentifier, "notebook");
    7893    }
    7994
    80     static public String TARGET_NOT_FOUND(String externalIdentifier) {
     95    private MessageStatus  _TARGET_NOT_FOUND(String externalIdentifier) {
    8196        return resourceNotFound(externalIdentifier, "target");
    8297    }
    8398
    84     static public String CACHED_REPRESENTATION_NOT_FOUND(String externalIdentifier) {
     99    private MessageStatus  _CACHED_REPRESENTATION_NOT_FOUND(String externalIdentifier) {
    85100        return resourceNotFound(externalIdentifier, "cached representation");
    86101    }
    87102
    88     static public String INVALID_PERMISSION_MODE(String permissionMode) {
    89         return permissionMode + " is an invalid permission value, which must be either owner, or reader, or writer.";
    90     }
    91 
    92     public void sendFailureMessage(String message, int responseCode) throws IOException {
    93         logger.debug(responseCode + ": " + message);
    94         httpServletResponse.sendError(responseCode, message);
     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());
    95214    }
    96215}
Note: See TracChangeset for help on using the changeset viewer.