View Javadoc

1   /*
2    * Copyright 2010-2013 Capgemini Licensed under the Apache License, Version 2.0 (the
3    * "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package net.sf.appstatus.web.pages;
15  
16  import static org.apache.commons.io.IOUtils.copy;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import net.sf.appstatus.web.StatusWebHandler;
27  
28  public class Resources {
29  
30  	public static class ResourceDefinition {
31  		private final String location;
32  		private final String mimeType;
33  
34  		protected ResourceDefinition(String location, String mimeType) {
35  			this.location = location;
36  			this.mimeType = mimeType;
37  		}
38  
39  		public String getLocation() {
40  			return location;
41  		}
42  
43  		public String getMimeType() {
44  			return mimeType;
45  		}
46  	}
47  
48  	public static final String LOGO = "logo";
49  	private static Map<String, ResourceDefinition> resources = new HashMap<String, ResourceDefinition>();
50  	public static final String STATUS_ERROR = "error";
51  	public static final String STATUS_JOB = "job";
52  	public static final String STATUS_JOB_ERROR = "job-error";
53  	public static final String STATUS_JOB_WARNING = "job-warning";
54  	public static final String STATUS_OK = "ok";
55  	public static final String STATUS_PROP = "prop";
56  
57  	public static final String STATUS_WARN = "warn";
58  
59  	static {
60  		addResource(STATUS_OK, "/org/freedesktop/tango/22x22/status/weather-clear.png", "image/png");
61  		addResource(STATUS_WARN, "/org/freedesktop/tango/22x22/status/weather-overcast.png", "image/png");
62  		addResource(STATUS_ERROR, "/org/freedesktop/tango/22x22/status/weather-severe-alert.png", "image/png");
63  		addResource(STATUS_PROP, "/org/freedesktop/tango/22x22/actions/format-justify-fill.png", "image/png");
64  		addResource(STATUS_JOB, "/org/freedesktop/tango/22x22/emblems/emblem-system.png", "image/png");
65  		addResource(STATUS_JOB_ERROR, "/org/freedesktop/tango/22x22/status/dialog-error.png", "image/png");
66  		addResource(STATUS_JOB_WARNING, "/org/freedesktop/tango/22x22/status/dialog-warning.png", "image/png");
67  		addResource(LOGO, "/assets/img/appstatus-logo.png", "image/png");
68  	}
69  
70  	public static void addResource(String id, String location, String mimeType) {
71  		resources.put(id, new ResourceDefinition(location, mimeType));
72  	}
73  
74  	public static void doGet(StatusWebHandler webHandler, HttpServletRequest req, HttpServletResponse resp)
75  			throws IOException {
76  
77  		String location = null;
78  		String id = req.getParameter("resource");
79  		if (id == null) {
80  			id = req.getParameter("icon");
81  		}
82  
83  		if (resources.containsKey(id)) {
84  			resp.setContentType(resources.get(id).getMimeType());
85  			location = resources.get(id).getLocation();
86  			InputStream is = Resources.class.getResourceAsStream(location);
87  			copy(is, resp.getOutputStream());
88  		} else {
89  			resp.sendError(404);
90  		}
91  
92  	}
93  }