Search in sources :

Example 1 with SxDocumentSource

use of com.xpn.xwiki.web.sx.SxDocumentSource in project xwiki-platform by xwiki.

the class AbstractSxExportURLFactoryActionHandler method processSx.

private URL processSx(List<String> spaceNames, String name, String queryString, XWikiContext context, FilesystemExportContext exportContext) throws Exception {
    SxSource sxSource = null;
    // Check if we have the JAR_RESOURCE_REQUEST_PARAMETER parameter in the query string
    List<NameValuePair> params = URLEncodedUtils.parse(queryString, StandardCharsets.UTF_8);
    for (NameValuePair param : params) {
        if (param.getName().equals(JAR_RESOURCE_REQUEST_PARAMETER)) {
            sxSource = new SxResourceSource(param.getValue());
            break;
        }
    }
    if (sxSource == null) {
        sxSource = new SxDocumentSource(context, getExtensionType());
    }
    String content = getContent(sxSource, exportContext);
    // Write the content to file
    // We need a unique name for that SSX content
    String targetPath = String.format("%s/%s/%s", getSxPrefix(), StringUtils.join(spaceNames, '/'), name);
    File targetDirectory = new File(exportContext.getExportDir(), targetPath);
    if (!targetDirectory.exists()) {
        targetDirectory.mkdirs();
    }
    File targetLocation = File.createTempFile(getSxPrefix(), "." + getFileSuffix(), targetDirectory);
    FileUtils.writeStringToFile(targetLocation, content);
    // Rewrite the URL
    StringBuilder path = new StringBuilder("file://");
    // Adjust based on current document's location. We need to account for the fact that the current document
    // is stored in subdirectories inside the top level "pages" directory. Since the SX files are also in top
    // subdirectories, we need to compute the path to them.
    path.append(StringUtils.repeat("../", exportContext.getDocParentLevel()));
    path.append(getSxPrefix());
    path.append(URL_PATH_SEPARATOR);
    for (String spaceName : spaceNames) {
        path.append(encodeURLPart(spaceName));
        path.append(URL_PATH_SEPARATOR);
    }
    path.append(encodeURLPart(name));
    path.append(URL_PATH_SEPARATOR);
    path.append(encodeURLPart(targetLocation.getName()));
    return new URL(path.toString());
}
Also used : NameValuePair(org.apache.http.NameValuePair) SxSource(com.xpn.xwiki.web.sx.SxSource) File(java.io.File) SxResourceSource(com.xpn.xwiki.web.sx.SxResourceSource) SxDocumentSource(com.xpn.xwiki.web.sx.SxDocumentSource) URL(java.net.URL)

Aggregations

SxDocumentSource (com.xpn.xwiki.web.sx.SxDocumentSource)1 SxResourceSource (com.xpn.xwiki.web.sx.SxResourceSource)1 SxSource (com.xpn.xwiki.web.sx.SxSource)1 File (java.io.File)1 URL (java.net.URL)1 NameValuePair (org.apache.http.NameValuePair)1