KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webdav > PutMethod


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.webdav;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.alfresco.model.ContentModel;
27 import org.alfresco.service.cmr.model.FileExistsException;
28 import org.alfresco.service.cmr.model.FileFolderService;
29 import org.alfresco.service.cmr.model.FileInfo;
30 import org.alfresco.service.cmr.model.FileNotFoundException;
31 import org.alfresco.service.cmr.repository.ContentWriter;
32 import org.alfresco.service.namespace.QName;
33
34 /**
35  * Implements the WebDAV PUT method
36  *
37  * @author Gavin Cornwell
38  */

39 public class PutMethod extends WebDAVMethod
40 {
41     // Request parameters
42
private String JavaDoc m_strLockToken = null;
43     private String JavaDoc m_strContentType = null;
44     private boolean m_expectHeaderPresent = false;
45
46     /**
47      * Default constructor
48      */

49     public PutMethod()
50     {
51     }
52
53     /**
54      * Parse the request headers
55      *
56      * @exception WebDAVServerException
57      */

58     protected void parseRequestHeaders() throws WebDAVServerException
59     {
60         m_strContentType = m_request.getHeader(WebDAV.HEADER_CONTENT_TYPE);
61         String JavaDoc strExpect = m_request.getHeader(WebDAV.HEADER_EXPECT);
62
63         if (strExpect != null && strExpect.equals(WebDAV.HEADER_EXPECT_CONTENT))
64         {
65             m_expectHeaderPresent = true;
66         }
67
68         // Get the lock token, if any
69

70         m_strLockToken = parseIfHeader();
71     }
72
73     /**
74      * Parse the request body
75      *
76      * @exception WebDAVServerException
77      */

78     protected void parseRequestBody() throws WebDAVServerException
79     {
80         // Nothing to do in this method, the body contains
81
// the content it will be dealt with later
82
}
83
84     /**
85      * Exceute the WebDAV request
86      *
87      * @exception WebDAVServerException
88      */

89     protected void executeImpl() throws WebDAVServerException, Exception JavaDoc
90     {
91         FileFolderService fileFolderService = getFileFolderService();
92
93         // Get the status for the request path
94
FileInfo contentNodeInfo = null;
95         boolean created = false;
96         try
97         {
98             contentNodeInfo = getDAVHelper().getNodeForPath(getRootNodeRef(), getPath(), getServletPath());
99             // make sure that we are not trying to use a folder
100
if (contentNodeInfo.isFolder())
101             {
102                 throw new WebDAVServerException(HttpServletResponse.SC_BAD_REQUEST);
103             }
104         }
105         catch (FileNotFoundException e)
106         {
107             // the file doesn't exist - create it
108
String JavaDoc[] paths = getDAVHelper().splitPath(getPath());
109             try
110             {
111                 FileInfo parentNodeInfo = getDAVHelper().getNodeForPath(getRootNodeRef(), paths[0], getServletPath());
112                 // create file
113
contentNodeInfo = fileFolderService.create(parentNodeInfo.getNodeRef(), paths[1], ContentModel.TYPE_CONTENT);
114                 created = true;
115                 
116                 // apply the titled aspect - title and description
117
Map JavaDoc<QName, Serializable JavaDoc> titledProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(3, 1.0f);
118                 titledProps.put(ContentModel.PROP_TITLE, paths[1]);
119                 titledProps.put(ContentModel.PROP_DESCRIPTION, "");
120                 getNodeService().addAspect(contentNodeInfo.getNodeRef(), ContentModel.ASPECT_TITLED, titledProps);
121             }
122             catch (FileNotFoundException ee)
123             {
124                 // bad path
125
throw new WebDAVServerException(HttpServletResponse.SC_BAD_REQUEST);
126             }
127             catch (FileExistsException ee)
128             {
129                 // bad path
130
throw new WebDAVServerException(HttpServletResponse.SC_BAD_REQUEST);
131             }
132         }
133         
134         // Access the content
135
ContentWriter writer = fileFolderService.getWriter(contentNodeInfo.getNodeRef());
136         // set content properties
137
if (m_strContentType != null)
138         {
139             writer.setMimetype(m_strContentType);
140         }
141         else
142         {
143             String JavaDoc guessedMimetype = getServiceRegistry().getMimetypeService().guessMimetype(contentNodeInfo.getName());
144             writer.setMimetype(guessedMimetype);
145         }
146         // use default encoding
147
writer.setEncoding("UTF-8");
148
149         // Get the input stream from the request data
150
InputStream JavaDoc input = m_request.getInputStream();
151
152         // Write the new data to the content node
153
writer.putContent(input);
154
155         // Set the response status, depending if the node existed or not
156
m_response.setStatus(created ? HttpServletResponse.SC_CREATED : HttpServletResponse.SC_NO_CONTENT);
157     }
158 }
159
Popular Tags