NBImportPlugin
NBImport Plugin for JSPWiki
Motivation
We need to include documents from other netbeans sites in the wiki pages. It isn't possible with standard INCLUDE plugin because of security reasons.
Solution
NBImport plugin allows include ONLY and ONLY a page from netbeans.org domain in page at wiki. It will allow us to include documents in Hg and from other netbeans sites. To make the plugin even more safe the included page is in PRE tags.
NBImportPlugin usage:
[{NBImportPluginUrl=http://foo.netbeans.org/foo.bar}]
Error messages
- NBImportPlugin error: Wrong URL syntax.
- NBImportPlugin error: Plugin accepts only files from netbeans.org domain.
[{NBImportPluginUrl=http://hg.netbeans.org/main/rawFile/tip/api.java/manifest.mf}]
[[{NBImportPluginUrl=http://hg.netbeans.org/main/rawFile/tip/api.java/manifest.mf} | {NBImportPlugin url='http://hg.netbeans.org/main/raw-file/tip/api.java/manifest.mf'}]]
Source code
package nbimport;
import com.ecyrd.jspwiki.plugin.WikiPlugin;
import com.ecyrd.jspwiki.WikiContext;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
public class NBImportPlugin implements WikiPlugin {
public String execute(WikiContext Context, Map ParameterMap) {
String importedFileContent = new String();
String pathName = "";
if ((ParameterMap != null) && (ParameterMap.containsKey("url"))) {
pathName = (String) ParameterMap.get("url");
try {
StringBuffer inFileBuffer = new StringBuffer();
URL fileUrl = new URL(pathName);
String hostName = fileUrl.getHost();
String[] hostNameSplit = hostName.split("<br>.");
int hostNameLength = hostNameSplit.length;
if (hostNameLength >= 2) { // domain lenght check
if ((hostNameSplit[HostNameLength2].equals("netbeans")) &&
(hostNameSplit[HostNameLength1].equals("org"))) { // netbeans.org domain check
InputStream fileInStream = fileUrl.openStream();
BufferedReader fileBuff = new BufferedReader(new InputStreamReader(fileInStream));
String line;
while ((line = fileBuff.readLine()) != null) {
inFileBuffer.append(line + "\n");
}
importedFileContent = "<pre>\n" + inFileBuffer.toString() + "\n";
} else {
importedFileContent = "NBImportPlugin error: Plugin accepts only files from netbeans.org domain.";
}
} else {
importedFileContent = "NBImportPlugin error: Plugin accepts only files from netbeans.org domain.";
}
} catch (FileNotFoundException e) {
importedFileContent = "NBImportPlugin error: File not found.";
} catch (MalformedURLException u) {
importedFileContent = "NBImportPlugin error: Wrong URL syntax.";
} catch (IOException o) {
importedFileContent = "NBImportPlugin error: IO exception.";
}
} else {
importedFileContent = "NBImportPlugin usage:[{NBImportPluginUrl=http://foo.netbeans.org/foo.bar}]";
}
return importedFileContent;
}
}
</pre>

