View Javadoc

1   /*
2    * Copyright 2010-2012 Capgemini
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   *
15   */
16  package net.sf.appstatus.web.pages;
17  
18  import static net.sf.appstatus.web.HtmlUtils.applyLayout;
19  
20  import java.io.IOException;
21  import java.io.UnsupportedEncodingException;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.commons.lang3.text.StrBuilder;
29  
30  import net.sf.appstatus.web.IPage;
31  import net.sf.appstatus.web.StatusWebHandler;
32  
33  public abstract class AbstractPage implements IPage {
34  	private static final String ENCODING = "UTF-8";
35  
36  	private static final String PAGELAYOUT = "pageLayout.html";
37  
38  	private static final String URL = "http://appstatus.sourceforge.net/";
39  
40  	public abstract void doGet(StatusWebHandler webHandler, HttpServletRequest req, HttpServletResponse resp)
41  			throws UnsupportedEncodingException, IOException;
42  
43  	public abstract void doPost(StatusWebHandler webHandler, HttpServletRequest req, HttpServletResponse resp);
44  
45  	public String getId() {
46  		return null;
47  	}
48  
49  	public String getName() {
50  		return getId();
51  	}
52  
53  	protected String getPage(final StatusWebHandler webHandler, final Map<String, String> valueMap)
54  			throws UnsupportedEncodingException, IOException {
55  
56  		valueMap.put("css", "<link href=\"" + webHandler.getCssLocation() + "\" rel=\"stylesheet\">");
57  
58  		valueMap.put("UrlAppStatus", URL);
59  
60  		final StrBuilder menu = new StrBuilder();
61  		for (final String pageId : webHandler.getPages().keySet()) {
62  			final IPage page = webHandler.getPages().get(pageId);
63  			if (StringUtils.equals(pageId, getId())) {
64  				menu.append("<li class=active>");
65  			} else {
66  				menu.append("<li>");
67  			}
68  			menu.append("<a href=\"?p=" + page.getId() + "\">" + page.getName() + "</a></li>");
69  		}
70  		valueMap.put("menu", menu.toString());
71  		valueMap.put("applicationName", webHandler.getApplicationName());
72  
73  		if (!valueMap.containsKey("js")) {
74  			valueMap.put("js", "");
75  		}
76  
77  		return applyLayout(valueMap, PAGELAYOUT);
78  
79  	}
80  
81  	protected void setup(final HttpServletResponse resp, final String type) throws IOException {
82  		resp.setContentType(type);
83  		resp.setCharacterEncoding(ENCODING);
84  	}
85  }