Include local file content on page

How shall I include in XWiki Page file localy stored on a server (/private/info.txt). The Browser cannot access that file. (no file:/// is possible)? XWiki shall include a content of that file in page.

The local file cannot be accessed vie httpd as XWiki shall guard access controls (i.e. only some registered users can see it).

The scenario mentioned in https://forum.xwiki.org/t/image-from-local-storage/4577 mostly relates to client side, i.e browser needs access to image/file.

Hi, you can do that using a UNC url. See Wiki generates appropriate UNC path but link doesn't do anything

With UNC the browser will open connection to file:///somewhere/something.txt but my requirements is that XWiki/tomcat shall open connection to that file (e.g. read it localy) and place content of it in a page.

You could try:

{{velocity}}
$xwiki.getURLContent('file:///...')
{{/velocity}}

Note: Requires Programming Rights

The velocity macro $xwiki.getURLContent(‘file:///…’) did not read out file (file:///private/info.txt).
I am looking for some $xwiki command that calls “String text = Files.readString(Paths.get(filename));”.

That should work fine normally (if you have PR). I see no reason for it to fail except if you have some security restrictions enabled, or if you use a wrong URL.

Hi I manage it with following groovy Code:

{{groovy}}
String fileContents = new File('/tmp/test.txt').text
println(fileContents)
{{/groovy}}

Ok I’ve tested it with a file URL and it fails because of (you should also have had this error in your xwiki logs BTW):

2021-05-05 15:11:37,133 [qtp5592464-14 - http://localhost:8080/xwiki/bin/view/xxx/] WARN  c.x.x.a.XWiki                  - Failed to retrieve content from [file:///tmp/vincent.txt] 
java.lang.IllegalStateException: unsupported protocol: 'file'
	at org.apache.commons.httpclient.protocol.Protocol.lazyRegisterProtocol(Protocol.java:149)
	at org.apache.commons.httpclient.protocol.Protocol.getProtocol(Protocol.java:117)
	at org.apache.commons.httpclient.HttpHost.<init>(HttpHost.java:107)
	at org.apache.commons.httpclient.HttpMethodBase.setURI(HttpMethodBase.java:280)
	at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:220)
	at org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:89)
	at com.xpn.xwiki.XWiki.getURLContent(XWiki.java:6700)
	at com.xpn.xwiki.XWiki.getURLContent(XWiki.java:6693)
	at com.xpn.xwiki.api.XWiki.getURLContent(XWiki.java:2426)

So indeed my solution doesn’t work.

I have seen the same Warning in catalina.out

[https://testserver] WARN c.x.x.a.XWiki - Failed to retrieve content from [file:///tmp/test.txt]
java.lang.IllegalStateException: unsupported protocol: ‘file’
at org.apache.commons.httpclient.protocol.Protocol.lazyRegisterProtocol(Protocol.java:149)
at org.apache.commons.httpclient.protocol.Protocol.getProtocol(Protocol.java:117)
at org.apache.commons.httpclient.HttpHost.(HttpHost.java:107)

Thanks for help