View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2010 Frederico Caldeira Knabben
4    * 
5    * == BEGIN LICENSE ==
6    * 
7    * Licensed under the terms of any of the following licenses at your
8    * choice:
9    * 
10   *  - GNU General Public License Version 2 or later (the "GPL")
11   *    http://www.gnu.org/licenses/gpl.html
12   * 
13   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   *    http://www.gnu.org/licenses/lgpl.html
15   * 
16   *  - Mozilla Public License Version 1.1 or later (the "MPL")
17   *    http://www.mozilla.org/MPL/MPL-1.1.html
18   * 
19   * == END LICENSE ==
20   */
21  package net.fckeditor.tags;
22  
23  import java.io.IOException;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.jsp.JspException;
29  import javax.servlet.jsp.JspTagException;
30  import javax.servlet.jsp.JspWriter;
31  import javax.servlet.jsp.tagext.TagSupport;
32  
33  /*import org.slf4j.Logger;
34   import org.slf4j.LoggerFactory;*/
35  
36  import net.fckeditor.handlers.RequestCycleHandler;
37  import net.fckeditor.localization.LocalizedMessages;
38  import net.fckeditor.tool.Compatibility;
39  
40  /**
41   * Displays information about browser and user capabilities.
42   * <p>
43   * There are four available commands:
44   * <ol>
45   * <li><code>CompatibleBrowser</code></li>
46   * <li><code>CreateFolder</code></li>
47   * <li><code>FileUpload</code></li>
48   * <li><code>GetResources</code></li>
49   * </ol>
50   * </p>
51   * Each call will emit a localized message (if available) about the command's
52   * status.
53   * 
54   * @version $Id: CheckTag.java 4812 2009-12-23 21:14:49Z mosipov $
55   */
56  public class CheckTag extends TagSupport {
57  
58  	private static final long serialVersionUID = -6834095891675681686L;
59  
60  	private static final String FILE_UPLOAD = "FileUpload";
61  	private static final String GET_RESOURCES = "GetResources";
62  	private static final String CREATE_FOLDER = "CreateFolder";
63  	private static final String COMPATIBLE_BROWSER = "CompatibleBrowser";
64  
65  	private static final Set<String> commands = new HashSet<String>(4);
66  
67  	static {
68  		commands.add(FILE_UPLOAD);
69  		commands.add(COMPATIBLE_BROWSER);
70  		commands.add(GET_RESOURCES);
71  		commands.add(CREATE_FOLDER);
72  	}
73  
74  	private String command;
75  
76  	/**
77  	 * Sets the desired command.
78  	 * 
79  	 * @param command
80  	 *            a command to check
81  	 * @throws JspTagException
82  	 *             if the provided command does not exist
83  	 */
84  	public void setCommand(String command) throws JspTagException {
85  		if (!commands.contains(command))
86  			throw new JspTagException(
87  					"You have to provide one of the following commands: "
88  							+ commands);
89  		this.command = command;
90  	}
91  
92  	@Override
93  	public int doStartTag() throws JspException {
94  		JspWriter out = pageContext.getOut();
95  
96  		HttpServletRequest request = (HttpServletRequest) pageContext
97  				.getRequest();
98  		LocalizedMessages lm = LocalizedMessages.getInstance(request);
99  		String response = null;
100 
101 		if (command.equals(FILE_UPLOAD)) {
102 			if (RequestCycleHandler.isFileUploadEnabled(request))
103 				response = lm.getFileUploadEnabled();
104 			else
105 				response = lm.getFileUploadDisabled();
106 		}
107 
108 		if (command.equals(GET_RESOURCES)) {
109 			if (RequestCycleHandler.isGetResourcesEnabled(request))
110 				response = lm.getGetResourcesEnabled();
111 			else
112 				response = lm.getGetResourcesDisabled();
113 		}
114 
115 		if (command.equals(CREATE_FOLDER)) {
116 			if (RequestCycleHandler.isCreateFolderEnabled(request))
117 				response = lm.getCreateFolderEnabled();
118 			else
119 				response = lm.getCreateFolderDisabled();
120 		}
121 
122 		if (command.equals(COMPATIBLE_BROWSER)) {
123 			if (Compatibility.isCompatibleBrowser(request))
124 				response = lm.getCompatibleBrowserYes();
125 			else
126 				response = lm.getCompatibleBrowserNo();
127 		}
128 
129 		try {
130 			out.print(response);
131 		} catch (IOException e) {
132 			throw new JspException(
133 					"Tag response could not be written to the user!", e);
134 		}
135 
136 		return SKIP_BODY;
137 	}
138 
139 }