[opencms-dev] Issues programmatically creating resources

Michael Emmerich m.emmerich at alkacon.com
Wed Jun 22 09:15:32 CEST 2011


Yves,

the problem is in the following code:

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

By doing so, you log in as the user "contentcreator", but you are in the 
Online project. And there, you cannot create a resource of course.

So you must at least add the following lines after this code:



[...]
CmsObject cms = OpenCms.initCmsObject("Guest");
cms.loginUser("contentcreator", "thepassword");
CmsProject project = cms.readProject("Offline");
cms.getRequestContext().setCurrentProject(project);
[...]

By this, you change to the Offline project. You also might have to 
change to the correct site, so you can use the setSiteRoot Method in the 
request context.

It would be a good idea to check what you have in the request context 
after you initialized the CmsObect (cms.getRequestContext()) to see how 
your current request is initialized.


Kind Regards,
Michael.

Am 21.06.2011 22:57, schrieb Yves Glodt:
> Bunp... really no OpenCms-Api expert around ? :-)
>
> On 18 June 2011 15:21, Yves Glodt <yg at mind.lu <mailto:yg at mind.lu>> wrote:
>
>     Bump :-)
>
>
>     On 16 June 2011 09:48, Yves Glodt <yg at mind.lu <mailto:yg at mind.lu>>
>     wrote:
>
>         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() );
>         }
>
>         %>
>
>
>
>
>
>
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://lists.opencms.org/mailman/listinfo/opencms-dev

-- 
Kind Regards,
Michael.

-------------------
Michael Emmerich

Alkacon Software GmbH  - The OpenCms Experts
http://www.alkacon.com - http://www.opencms.org



More information about the opencms-dev mailing list