[opencms-dev] Issues programmatically creating resources

Yves Glodt yg at mind.lu
Thu Jun 16 09:48:47 CEST 2011


Hi there,

I am currently trying to do a interface to a Website where you can import
files to the VFS with an http post.

Unfortunately it does not work, and I do not find why. I get an exception
with the only message "Error creating the resource "/thepath/".

Here is the code, maybe someone spots the error :-)

Best regards,
Yves



<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" session="false" %>
<%@ page import="java.io.*, java.lang.*, java.util.*, java.text.*" %>
<%@ page import="javax.servlet.*, javax.servlet.http.*" %>
<%@ page import="org.opencms.*, org.opencms.main.*, org.opencms.flex.*,
org.opencms.jsp.*, org.opencms.file.*, org.opencms.file.types.*" %>
<%@ page import="org.apache.commons.lang.*" %>
<%@ page import="org.apache.commons.fileupload.*,
org.apache.commons.fileupload.disk.*,
org.apache.commons.fileupload.servlet.*" %>
<%

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);

List<FileItem> items = null;

try {
    items = upload.parseRequest(request);
} catch (FileUploadException e) {
    out.println("Error parsing request");
    System.out.println(request.getRemoteAddr()+": Import: Error parsing
request.");
    return;
}

Iterator<FileItem> iter = items.iterator();

Integer no_dossier = null;
String no_article = null;
String filename = null;
byte[] bytes = null;

while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();

    if (item.isFormField()) {

        if (item.getFieldName().equals("no_dossier")) {
            no_dossier = Integer.parseInt(item.getString());
        }

        if (item.getFieldName().equals("no_article")) {
            no_article = item.getString();
        }

    } else {
        filename = item.getName();
        bytes = item.get();
    }
}

try {

    CmsObject cms = OpenCms.initCmsObject("Guest");
    cms.loginUser("contentcreator", "thepassword");

    if (!cms.existsResource("/images")) {
        System.out.println("Creating /images");
        cms.createResource("/images",
CmsResourceTypeFolder.RESOURCE_TYPE_ID);
    }

    if (!cms.existsResource("/images/articles")) {
        System.out.println("Creating /images/articles");
        cms.createResource("/images/articles",
CmsResourceTypeFolder.RESOURCE_TYPE_ID);
    }

    if (!cms.existsResource("/images/articles/"+no_dossier)) {
        System.out.println("Creating /images/articles/"+no_dossier);
        cms.createResource("/images/articles/"+no_dossier,
CmsResourceTypeFolder.RESOURCE_TYPE_ID);
    }

    if (!cms.existsResource("/images/articles/"+no_dossier+"/"+no_article))
{
        System.out.println("Creating
/images/articles/"+no_dossier+"/"+no_article);
        cms.createResource("/images/articles/"+no_dossier+"/"+no_article,
CmsResourceTypeFolder.RESOURCE_TYPE_ID);
    }

    String newResname =
cms.getRequestContext().getFileTranslator().translateResource("/images/articles/"+no_dossier+"/"+no_article+"/"
+ filename);
    int resTypeId =
OpenCms.getResourceManager().getDefaultTypeForName(filename).getTypeId();
    cms.createResource(newResname, resTypeId, bytes, null);

    cms.unlockResource("/images/articles/"+no_dossier);
    cms.unlockResource("/images/articles/"+no_dossier+"/"+no_article);

    //cms.publishResource(newResname);
    //OpenCms.getPublishManager().publishResource(cms, folder);
    //OpenCms.getPublishManager().waitWhileRunning();

} catch (Exception e) {
    out.println("Error: "+e.getMessage() );
}

%>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20110616/4f8440a3/attachment.htm>


More information about the opencms-dev mailing list