dataportal release v2.0
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / HttpStatusMessage.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.remote.controller;
11
12 import java.io.IOException;
13 import java.util.Hashtable;
14
15 import javax.servlet.http.HttpServletResponse;
16
17 import org.apache.commons.lang.StringUtils;
18
19
20 /**
21 * @author a.kohlbecker
22 * @date 06.04.2009
23 *
24 */
25 public class HttpStatusMessage {
26
27 public final static HttpStatusMessage UUID_MISSING = new HttpStatusMessage(HttpServletResponse.SC_BAD_REQUEST, "missing uuid parameter");
28 public final static HttpStatusMessage UUID_INVALID = new HttpStatusMessage(HttpServletResponse.SC_BAD_REQUEST, "invalid uuid");
29 public final static HttpStatusMessage UUID_NOT_FOUND = new HttpStatusMessage(HttpServletResponse.SC_NOT_FOUND, "uuid not found");
30 public final static HttpStatusMessage UUID_REFERENCES_WRONG_TYPE = new HttpStatusMessage(HttpServletResponse.SC_NOT_FOUND, "uuid references wrong type");
31
32
33
34 private int statusCode;
35
36 private String message;
37
38 private String statusMessage;
39
40 private static Hashtable<String , HttpStatusMessage> cache;
41
42 private HttpStatusMessage(int statusCode, String message) {
43 this.statusCode = statusCode;
44 this.message = message;
45 this.statusMessage = StringUtils.leftPad(Integer.toString(statusCode), 3, "0") + message;
46 if(cache == null) {
47 cache = new Hashtable<String , HttpStatusMessage>();
48 }
49 HttpStatusMessage.cache.put(statusMessage, this);
50 }
51
52 public int getStatusCode() {
53 return statusCode;
54 }
55
56 public String getMessage() {
57 return message;
58 }
59
60 public String toString() {
61 return statusMessage;
62 }
63
64 public static HttpStatusMessage fromString(String statusMessage) {
65 return cache.get(statusMessage);
66 }
67
68 public void send(HttpServletResponse response) throws IOException{
69 response.sendError(getStatusCode(), getMessage());
70 }
71
72 }