0

Image from db with ZkSpring

asked 2010-03-05 05:29:03 +0800

btakacs gravatar image btakacs
153 3 4

I would need to upload images from a secure (https) admin page to the db and then show them on the unsecure common http pages that are for visitors.
On upload there is an url which is generated for the image, but it is not suitable for the visitors (since it is https).

I tried to change the url and create a servlet, that provides the images on /image/* but I had a problem to get the SpringContext.
If i use the ZKSpring way ((ImageDAO)SpringUtil.getApplicationContext().getBean("imageDao");), I get the following exception:
org.zkoss.zk.ui.UiException: SpringUtil can be called only under ZK environment!
at org.zkoss.zkplus.spring.SpringUtil.getApplicationContext(SpringUtil.java:43)
If I use the standard Spring way, I get the following:
java.lang.ClassCastException: $Proxy26 cannot be cast to my.cmsTest.dao.ImageDAO

Richlets are suitable for dynamic resource handling, but as i understand, it has to create a zul Page.

Extendlets can only be initilized from zk core, as I see.

Is there a solution for this? ( I'm sure there are more than one... :-) )

Thanks:
Bence

delete flag offensive retag edit

9 Replies

Sort by » oldest newest

answered 2010-03-10 08:56:47 +0800

btakacs gravatar image btakacs
153 3 4

I don't believe that nobody thinks of storing resources in db with the help of Spring.

Anybody?

link publish delete flag offensive edit

answered 2010-03-10 15:11:38 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2010-03-10 17:01:18 +0800

Your Question is not clearly.
Spring is too complex as to explain it in a few lines.

You must read self about the spring framework. And of course, spring is not an ORM it has helper classes for easier handling of some ORM frameworks and their CRUD operations.
After reading you can have a look at the codes of the Zksample2 demo application and their documentation.
It uses spring and hibernate heavily. And YES we have self spending MUCH time in doc reading and probing that it runs! So spend time to have a look at the structure of the packages and all of the configuration files.

best
Stephan

link publish delete flag offensive edit

answered 2010-03-11 07:38:35 +0800

btakacs gravatar image btakacs
153 3 4

Hi, Terry

I didn't know about this project before, and I'm happy about that. I checked the code and the documentation, and it seems to be a very interesting one.

But I see nothing relevant.

I would like to store resources in the db, and I would need to make them accessible through some kind of dynamic db resource provider. I'm developing a small CMS system. I have a zk page with a menu, and has a <html /> element, which gets the html-formatted content from db. Actually the whole menu structure comes from the db, and fully editable from the admin pages. I use Spring and hibernate data-access and use spring security for authorization (authenticating through db too).

My problem is: how can I display an uploaded image for visitors. I upload the image through a https page, and I store it's link (src) to the content of the page. But in ZK this source url is generated, and supported by ZK, and working only for logged in users, since it is https (and I think this url exists only until the Image object itself exists), and I don't know how this resource link generation is working. So I would need a fix url, which is http and does not depend the object but on the db. For example http://<domainname>/image/myPhoto.jpg will call this provider and get file called myphoto.jpg from db.

My first tip was to create a Servlet which can access the db and provide the resources, but get the exceptions above. Could you suggest an other solution for the general problem, or just for accessing ZKspring (SpringUtils) from a Servlet?

Well, if you can suggest me a documentation, it is good for me. But I did not found any relevant in the application, or the documentation. Maybe I missed something. I checked what I found about resources, and spring context and I will do further investigations, but it would be easier, if I would get some tips.

There is a common way in ZK to provide resources, but I did not see any easy way to extend this.
For example, Richlets and Extendlets are for similar reasons, but I cannot see how can I do this work with them.

Well, I read documentations too. Believe me, I bought ZK Developer’s Guide, which is quite out-of-date, and read ZK online documentations, the forum, and investigating the source too. I just didn't figured out the solution so far, and asking for help.

Regards:
Bence

link publish delete flag offensive edit

answered 2010-03-11 14:48:08 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Hi Bence,

have you had a look here . Maik have developed a CMS system with zk. Perhaps you can find there any ideas.
I haven't the time for the next days. Make a Push end next week if your problem still exists.

best
Stephan

link publish delete flag offensive edit

answered 2010-03-12 06:54:03 +0800

btakacs gravatar image btakacs
153 3 4

Thanks, Stephan

I'm checking, but at first glimpse, the thing is that Jease does not use Spring, nor Relational db.

And he provides all file resources through jsp.

File.jsp:

<%@page import="java.io.FileInputStream,org.apache.commons.io.*,jease.cms.domain.*"%>
<%
	File file = (File) request.getAttribute("Node");		
	response.setContentLength((int) file.getFile().length());
	response.setContentType(file.getContentType());
	if(file.getContentType().startsWith("text")) {
		IOUtils.copy(new FileInputStream(file.getFile()), response.getWriter());
	} else {
		IOUtils.copy(new FileInputStream(file.getFile()), response.getOutputStream());
	}
%>

Regards:
Bence

link publish delete flag offensive edit

answered 2010-03-23 15:32:00 +0800

btakacs gravatar image btakacs
153 3 4

Hi, Stephan

I don't know what Push means, but I did not solve the "files from db" issue. Do you have any tips?

Regards:
Bence

link publish delete flag offensive edit

answered 2010-04-01 20:46:10 +0800

btakacs gravatar image btakacs
153 3 4

HI, All

I added a custom AuExtension by modifiing the web.xml:

<servlet>
		<description>The asynchronous update engine for ZK</description>
		<servlet-name>auEngine</servlet-name>
		<servlet-class>
		org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
		<init-param>
			<param-name>extension0</param-name>
			<param-value>/media=my.cmsTest.service.DynamicDatabaseMedia</param-value>	
		</init-param>
	</servlet>

but:
I'm still in need of a valid execution, since it is needed to use the SpringUtil in ZK Spring.
HOw can I do that?

The AuDynaMediar do the following:

final Session sess = Sessions.getCurrent(false);
		if (sess == null) {
			response.sendError(response.SC_GONE, Messages.get(MZk.PAGE_NOT_FOUND, pi));
			return;
		}
...
final WebApp wapp = sess.getWebApp();
final WebAppCtrl wappc = (WebAppCtrl)wapp;
final UiEngine uieng = wappc.getUiEngine();
final Desktop desktop = wappc.getDesktopCache(sess).getDesktop(dtid);
final DesktopCtrl desktopCtrl = (DesktopCtrl)desktop;

final Execution oldexec = Executions.getCurrent();
final Execution exec = new ExecutionImpl(
		ctx, request, response, desktop, null);
uieng.activate(exec);

final Configuration config = wapp.getConfiguration();
...
config.invokeExecutionInits(exec, oldexec);
desktopCtrl.invokeExecutionInits(exec, oldexec);

But for that I will need a session and a desktop id. The ZK stores the desktop id in the url by default, but in a forum link I see no sign of it (http://zh.zkoss.org/forum/listComment/11138;jsessionid=CBECE6FA0891992AD9D06D7197D49567.zkzh).
And if you want your webpage to be crawled by search bots, you can manage theese things without storing sessions (or maybe store them in the page link like: jsessionid=CBECE6FA0891992AD9D06D7197D49567.zkzh).
Since the forum is google search compatible, and I don't see any desktop ID in the links there how could you solve this issues in the ZK forum?

Thanks:
Bence

link publish delete flag offensive edit

answered 2010-04-02 09:13:37 +0800

btakacs gravatar image btakacs
153 3 4

Hi, All

I tried many things, but I wasn't succeeded.

Is there a way to get a spring bean from ZK spring? Without a desktopid and an userid? Or at least some default desktop, and anonymous user...
Many times it would needed.

Again:
If I call the ZK way (SpringUtil.getApplicationContext()) without an active Execution (what needs a desktopid and a userid afaik), I get the following exception:
UiException: SpringUtil can be called only under ZK environment!

...and if I try the Spring way (WebApplicationContextUtils.getRequiredWebApplicationContext(ctx) then I get the following exception:
java.lang.ClassCastException: $Proxy25 cannot be cast to my.cmsTest.dao.ImageDAO
at my.cmsTest.service.DynamicDatabaseMedia.service(DynamicDatabaseMedia.java:63)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:415)

Any tips?

Thanks:
Bence

link publish delete flag offensive edit

answered 2010-04-28 15:22:04 +0800

btakacs gravatar image btakacs
153 3 4

updated 2010-04-29 02:19:30 +0800

Hi

Actualy this is a Spring issue: if you create an interface for the entity (I mean the entity implements one), then Spring automatically switch to JDK Dynamic Proxy.

There are two ways to solve this: use the interface where it is enough, or force using CGlib Proxies.

See: http://static.springsource.org/spring/docs/2.5.x/reference/aop.html#aop-proxying

Regards:
Bence

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-03-05 05:29:03 +0800

Seen: 1,157 times

Last updated: Apr 28 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More