KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > blogs > actions > NewMediaObjectAction


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution Sàrl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * ----- END LICENSE BLOCK -----
36  */

37
38 package org.jahia.blogs.actions;
39
40 import org.jahia.blogs.model.MediaObject;
41
42 import org.jahia.blogs.ServletResources;
43
44 import org.jahia.services.webdav.JahiaWebdavBaseService;
45 import org.jahia.services.webdav.DAVFileAccess;
46
47 import org.jahia.services.usermanager.JahiaUser;
48
49 import org.jahia.exceptions.JahiaException;
50
51 import org.apache.log4j.Logger;
52
53 import java.util.Hashtable JavaDoc;
54
55 import java.io.File JavaDoc;
56 import java.io.FileOutputStream JavaDoc;
57 import java.io.IOException JavaDoc;
58
59 /**
60  * Action used to upload a file to the Jahia content repository.
61  * Compliant with MetaWeblog API's newMediaObject method.
62  *
63  * @author Xavier Lawrence
64  */

65 public class NewMediaObjectAction extends AbstractAction {
66     
67     public static final String JavaDoc WEBDAV_CONTEXT = "/jahia/webdav";
68     
69     // log4j logger
70
static Logger log = Logger.getLogger(NewMediaObjectAction.class);
71     
72     private String JavaDoc blogID;
73     private Hashtable JavaDoc struct;
74     
75     /** Creates a new instance of NewMediaObjectAction */
76     public NewMediaObjectAction(String JavaDoc blogID, String JavaDoc userName,
77             String JavaDoc password, Hashtable JavaDoc struct) {
78         
79         super.userName = userName;
80         super.password = password;
81         this.blogID = blogID;
82         this.struct = struct;
83     }
84     
85     /**
86      * Uploads a media object into Jahia.
87      *
88      * @return A Hashtable containing the URL of the uploaded object
89      */

90     public Object JavaDoc execute() throws JahiaException {
91         
92         // Create commmon resources
93
super.init();
94         
95         // First check that the user is registered to this site.
96
final JahiaUser user = super.checkLogin();
97         
98         // Set the correct page and check write access
99
if (!super.changePage(Integer.parseInt(blogID)).checkWriteAccess(user)) {
100             throw new JahiaException(
101                     "You do not have write access to Blog: "+blogID,
102                     "You do not have write access to Page: "+blogID,
103                     JahiaException.ACL_ERROR,
104                     JahiaException.WARNING_SEVERITY);
105         }
106         
107         String JavaDoc dir = "/users/" + user.getUsername() + "/public";
108         
109         final DAVFileAccess dav = JahiaWebdavBaseService.getInstance().
110                 getDAVFileAccess(jParams, jParams.getSite(), user, dir);
111         
112         dav.beginTransaction();
113         
114         try {
115             File JavaDoc f = File.createTempFile("blogUpload", ".tmp");
116             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(f);
117             fos.write((byte[])struct.get(MediaObject.BITS));
118             fos.flush();
119             fos.close();
120             
121             log.debug("blogUpload TmpFile: "+f.getName());
122             
123             final String JavaDoc contentType = (String JavaDoc)struct.get(MediaObject.TYPE);
124             final String JavaDoc finalName = dav.uploadFile((String JavaDoc)struct.get(
125                     MediaObject.NAME), f, contentType, false);
126             
127             dav.commitTransaction();
128             
129             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
130             buffer.append(ServletResources.getCurrentRequest().
131                     getScheme());
132             buffer.append("://");
133             buffer.append(ServletResources.getCurrentRequest().
134                     getServerName());
135             buffer.append(":");
136             buffer.append(ServletResources.getCurrentRequest().
137                     getServerPort());
138             buffer.append(WEBDAV_CONTEXT);
139             buffer.append(dir);
140             buffer.append("/");
141             buffer.append(finalName);
142             
143             Hashtable JavaDoc result = new Hashtable JavaDoc(1);
144             result.put(MediaObject.URL, buffer.toString());
145             
146             log.debug("Media Object URL is: "+result);
147             f.delete();
148             return result;
149             
150         } catch (IOException JavaDoc e) {
151             throw new JahiaException(e.getMessage(), e.getMessage(),
152                     JahiaException.APPLICATION_ERROR,
153                     JahiaException.ERROR_SEVERITY, e);
154         }
155     }
156 }
157
Popular Tags