jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Douglas José" <douglasj...@gmail.com>
Subject Re: question about how to provide download link after save file into JCR
Date Tue, 30 Sep 2008 09:21:51 GMT
Hello Alexander and shrimpywu,

Not satisfied with using WebDAV to download a file from my repository
(I could not easily prevent the user from being prompted for
credentials), I wrote my own download servlet (the option 2 suggested
by Alexander).
Following is the source code. To use it, you just need to render a
link to the servlet mapping, appending the path in your repository to
the node to be downloaded.

Regards,

Douglas

package br.com.hapkidocontato.site.presentation;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.jcr.Session;
import javax.jcr.Item;
import javax.jcr.RepositoryException;
import javax.jcr.Node;
import java.io.IOException;
import java.io.InputStream;

import br.com.hapkidocontato.site.business.repository.RepositoryConfigurator;

/**
 * @author Douglas Rodrigues
 */

public class RepositoryDownloadServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request,
HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);

    }

    protected void doGet(HttpServletRequest request,
HttpServletResponse response)
            throws ServletException, IOException {
        WebApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        Session session = ((Session) ctx.getBean("jcrSession"));
        String path = request.getPathInfo();
        try {
            Node node = (Node) session.getItem(path);
            if (!node.getPrimaryNodeType().getName().equalsIgnoreCase("nt:file"))
{
                throw new ServletException("Impossible to download
from nodetype "
                        + node.getPrimaryNodeType().getName() + ".
Only nt:file accepted.");
            }
            InputStream is =
node.getNode("jcr:content").getProperty("jcr:data").getValue().getStream();
            IOUtils.copy(is, response.getOutputStream());
        } catch (RepositoryException e) {
            throw new ServletException("Error reading content", e);
        }
    }
}

On Tue, Sep 30, 2008 at 9:20 AM, Alexander Klimetschek <aklimets@day.com> wrote:
>
> You could use the WebDav server [1] (use the SimpleWebdavServlet) that
> comes with the jackrabbit-webapp to access the repository over
> Http/Webdav.
>
> Or you could program your own servlet that reads the file data using
> the JCR API and provides it over HTTP.
>
> Regards,
> Alex
>
> [1] http://jackrabbit.apache.org/jackrabbit-jcr-server.html
>
> On Tue, Sep 30, 2008 at 4:42 AM, shrimpywu <imxmwu@gmail.com> wrote:
> >
> > question about how to provide download link after save file into JCR
> >
> > i wrote a function to save binary file,  but after i save the file. how can
> > i get the file????????
> >
> > [code]
> > public String saveFile(final File file, final String mimeType, final String
> > encoding, final User user) {
> >        return (String) execute(new JcrCallback() {
> >
> >            public Object doInJcr(Session session) throws IOException,
> > RepositoryException {
> >
> >                checkUserFolder(user, session);
> >
> >                Node root = session.getRootNode();
> >
> >                JcrConstants jcrConstants = new JcrConstants(session);
> >
> >                //create the file node
> >                Node fileNode =
> > root.getNode(USER_FOLDER_PREFIX).getNode(user.getUsername()).addNode(file.getName(),
> > jcrConstants.getNT_FILE());
> >
> >                //create the mandatory child node - jcr:content
> >                Node resourseNode =
> > fileNode.addNode(jcrConstants.getJCR_CONTENT(),
> > jcrConstants.getNT_RESOURCE());
> >
> >                resourseNode.setProperty(jcrConstants.getJCR_MIMETYPE(),
> > mimeType);
> >                resourseNode.setProperty(jcrConstants.getJCR_ENCODING(),
> > encoding);
> >                resourseNode.setProperty(jcrConstants.getJCR_DATA(), new
> > FileInputStream(file));
> >                Calendar lastModified = Calendar.getInstance();
> >                lastModified.setTimeInMillis(file.lastModified());
> >                resourseNode.setProperty(jcrConstants.getJCR_LASTMODIFIED(),
> > lastModified);
> >
> >                session.save();
> >
> >
> > log.debug(resourseNode.getCorrespondingNodePath(session.getWorkspace().getName()));
> >                return resourseNode.getPath();
> >            }
> >        });
> >    }
> > [/code]
> >
> > Here is the bean i used to inject into spring.
> > [code]
> > <bean id="repository"
> >        class="org.springmodules.jcr.jackrabbit.RepositoryFactoryBean">
> >        <property name="configuration" value="classpath:/repository.xml" />
> >        <property name="homeDir" value="file:./target/repo" />
> >    </bean>
> > [/code]
> >
> > --
> > View this message in context: http://www.nabble.com/question-about-how-to-provide-download-link-after-save-file-into-JCR-tp19735243p19735243.html
> > Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
> >
> >
>
>
>
> --
> Alexander Klimetschek
> alexander.klimetschek@day.com



--
Douglas Jose
http://douglasjose.com

- "Use free software. Help us make a free world."

Mime
View raw message