[opencms-dev] Issues programmatically creating resources

Yves Glodt yg at mind.lu
Tue Jun 28 09:13:11 CEST 2011


Thanks go to Michael, I got it working with his suggestion. Here my final
code:



<%@ 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.*" %>
<%

if (!request.getMethod().equals("POST")) {
	response.sendRedirect("http://my.site.biz/");
	return;
}

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");

	CmsProject project = cms.readProject("Offline");
	cms.getRequestContext().setCurrentProject(project);

	cms.getRequestContext().setSiteRoot("/sites/my.site.biz/");
	

	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");
	//cms.unlockResource("/images/articles/"+no_dossier);
	//cms.unlockResource("/images/articles/"+no_dossier+"/"+no_article);

	cms.publishResource("/images/articles");
	//OpenCms.getPublishManager().publishResource(cms, folder);
	//OpenCms.getPublishManager().waitWhileRunning();

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

%>



On 22 June 2011 16:40, Deiverson Silveira <deiverson at solutioncms.com> wrote:

> Hi,
>
> I had a similar problem, was a contact form, which should
> create a structured content then submit contact form .
>
> The problem was the User logoff, and permission/security, because if user´s
> (a lot user´s) submit, if get logout in moment that a procedure is
> occurring.
>
> If I do not log out, a visitor could also log User /system/login/, and we
> would have a major security problem, because access to information not
> allowed.
>
> Other problem, maybe, if you give a logout and use your portal Session to
> something, because logout execute a session.invalidate().
>
> My Object, I did not know this form CmsObject OpenCms.initCmsObject cms =
> ("Guest"), I make this :
>         CmsObject cmsObject = cms.getCmsObject();
> CmsProject cmsproject = cmsObject.readProject("Offline");
>  cmsObject.getRequestContext().setCurrentProject(cmsproject);
> cmsObject.getRequestContext().setSiteRoot("/myproject");
>
> Regards,
>
> Deiverson Silveira
>
> 2011/6/22 Michael Emmerich <m.emmerich at alkacon.com>
>
>> 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
>>
>> _______________________________________________
>> 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
>>
>
>
>
> --
> *
> www.solutioncms.com
>  The Brazilian
> Official Provider OpenCms
> *
>
>
> _______________________________________________
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20110628/c8fdf50d/attachment.htm>


More information about the opencms-dev mailing list