View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2003-2008 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 net.fckeditor.handlers.PropertiesLoader;
34  import net.fckeditor.handlers.RequestCycleHandler;
35  import net.fckeditor.tool.Compatibility;
36  
37  /**
38   * This tag displays information about browser and user capabilities. There are
39   * tree available commands (CompatibleBrowser, FileBrowsing, FileUpload) which
40   * respond an English message.<br />
41   * <strong>Hint</strong>: The message string cannot be localized at the moment.
42   * 
43   * @version $Id: CheckTag.java 2151 2008-07-02 22:03:15Z mosipov $
44   */
45  public class CheckTag extends TagSupport {
46  
47  	private static final long serialVersionUID = -6834095891675681686L;
48  
49  	private static final String FILE_UPLOAD = "FileUpload";
50  	private static final String FILE_BROWSING = "FileBrowsing";
51  	private static final String COMPATIBLE_BROWSER = "CompatibleBrowser";
52  	private static final String PROPERTY_MESSAGE_FILE_BROWSING_DISABLED = "message.enabled_tag.connector.file_browsing.disabled";
53  	private static final String PROPERTY_MESSAGE_FILE_BROWSING_ENABLED = "message.enabled_tag.connector.file_browsing.enabled";
54  	private static final String PROPERTY_MESSAGE_FILE_UPLOAD_DISABLED = "message.enabled_tag.connector.file_upload.disalbed";
55  	private static final String PROPERTY_MESSAGE_FILE_UPLOAD_ENABLED = "message.enabled_tag.connector.file_upload.enabled";
56  	private static final String PROPERTY_MESSAGE_NOT_COMPATIBLE_BROWSER = "message.enabled_tag.compatible_browser.no";
57  	private static final String PROPERTY_MESSAGE_COMPATIBLE_BROWSER = "message.enabled_tag.compatible_browser.yes";
58  	private static Set<String> commands = new HashSet<String>(3);
59  
60  	static {
61  		commands.add(FILE_UPLOAD);
62  		commands.add(FILE_BROWSING);
63  		commands.add(COMPATIBLE_BROWSER);
64  	}
65  
66  	private String command;
67  
68  	/**
69  	 * Sets the desired command.
70  	 * @param command
71  	 * @throws JspTagException
72  	 */
73  	public void setCommand(String command) throws JspTagException {
74  		if (!commands.contains(command))
75  			throw new JspTagException("You have to provide one of the following commands: "
76  			        + commands);
77  		this.command = command;
78  	}
79  
80  	@Override
81  	public int doStartTag() throws JspException {
82  		JspWriter out = pageContext.getOut();
83  
84  		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
85  		String response = null;
86  
87  		if (command.equals(FILE_UPLOAD)) {
88  			if (RequestCycleHandler.isEnabledForFileUpload(request))
89  				response = PropertiesLoader
90  				        .getProperty(CheckTag.PROPERTY_MESSAGE_FILE_UPLOAD_ENABLED);
91  			else
92  				response = PropertiesLoader
93  				        .getProperty(CheckTag.PROPERTY_MESSAGE_FILE_UPLOAD_DISABLED);
94  		}
95  
96  		if (command.equals(FILE_BROWSING)) {
97  			if (RequestCycleHandler.isEnabledForFileBrowsing(request))
98  				response = PropertiesLoader
99  				        .getProperty(CheckTag.PROPERTY_MESSAGE_FILE_BROWSING_ENABLED);
100 			else
101 				response = PropertiesLoader
102 				        .getProperty(CheckTag.PROPERTY_MESSAGE_FILE_BROWSING_DISABLED);
103 		}
104 		if (command.equals(COMPATIBLE_BROWSER)) {
105 			if (Compatibility.isCompatibleBrowser(request))
106 				response = PropertiesLoader
107 				        .getProperty(CheckTag.PROPERTY_MESSAGE_COMPATIBLE_BROWSER);
108 			else
109 				response = PropertiesLoader
110 				        .getProperty(CheckTag.PROPERTY_MESSAGE_NOT_COMPATIBLE_BROWSER);
111 		}
112 
113 		try {
114 			out.print(response);
115 		} catch (IOException e) {
116 			throw new JspException("Tag response could not be written to the user!",e);
117 		}
118 
119 		return SKIP_BODY;
120 	}
121 
122 }