01: package de.java2html.plugin.jspwiki;
02:
03: import java.net.URL;
04:
05: import com.ecyrd.jspwiki.WikiContext;
06: import com.ecyrd.jspwiki.attachment.Attachment;
07: import com.ecyrd.jspwiki.attachment.AttachmentManager;
08: import com.ecyrd.jspwiki.plugin.PluginException;
09: import com.ecyrd.jspwiki.providers.ProviderException;
10:
11: /**
12: * @author Markus Gebhard
13: */
14: public class PluginSecurityManager {
15: private static final String FILE_URL_PROPERTY = "de.java2html.file.url.enabled";
16: private static final String HTTP_URL_PROPERTY = "de.java2html.http.url.enabled";
17:
18: private WikiContext context;
19:
20: public PluginSecurityManager(WikiContext context) {
21: this .context = context;
22: }
23:
24: public void checkUrlAccessEnabled(URL url) throws PluginException {
25: if ("file".equals(url.getProtocol())) {
26: if (!isPropertySetTrue(context, FILE_URL_PROPERTY)) {
27: throw new PluginException(
28: "File URLs are disabled in this Wiki (property '"
29: + FILE_URL_PROPERTY
30: + "' is not set to true).");
31: }
32: } else if ("http".equals(url.getProtocol())) {
33: if (!isPropertySetTrue(context, HTTP_URL_PROPERTY)) {
34: throw new PluginException(
35: "Http URLs are disabled in this Wiki (property '"
36: + HTTP_URL_PROPERTY
37: + "' is not set to true).");
38: }
39: } else {
40: throw new PluginException("Unsupported protocol: '"
41: + url.getProtocol() + "'");
42: }
43: }
44:
45: private boolean isPropertySetTrue(WikiContext context, String key) {
46: Object value = context.getEngine().getWikiProperties().get(key);
47: return value != null && "true".equals(value);
48: }
49:
50: public void checkValidAttachmentUrlPart(String attachment)
51: throws PluginException {
52: AttachmentManager attachmentManager = context.getEngine()
53: .getAttachmentManager();
54: if (!attachmentManager.attachmentsEnabled()) {
55: throw new PluginException(
56: "Attachments are not enabled in this Wiki.");
57: }
58: if (!attachmentManager.hasAttachments(context.getPage())) {
59: throw new PluginException(
60: "The current page does not have any attachments.");
61: }
62: Attachment attachmentInfo = null;
63: try {
64: attachmentInfo = attachmentManager.getAttachmentInfo(
65: context, attachment);
66: } catch (ProviderException e) {
67: throw new PluginException(
68: "The current page does not have an attachment '"
69: + attachment + "'");
70: }
71: if (attachmentInfo == null) {
72: throw new PluginException(
73: "The current page does not have an attachment '"
74: + attachment + "'");
75: }
76: }
77: }
|