View Javadoc

1   /*
2    * Copyright (c) 2006-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.webdav.spring.properties;
14  
15  import org.abstracthorizon.danube.http.Status;
16  import org.abstracthorizon.danube.webdav.ResourceAdapter;
17  import org.abstracthorizon.danube.webdav.xml.XMLParserHandler;
18  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.ResponseProperty;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.RandomAccessFile;
23  
24  import org.springframework.core.io.Resource;
25  
26  /**
27   * This property can set file's length.
28   *
29   * @author Daniel Sendula
30   */
31  public class GetContentLength extends org.abstracthorizon.danube.webdav.xml.dav.request.properties.GetContentLength {
32  
33      /**
34       * Constructor
35       * @param parent
36       */
37      public GetContentLength(XMLParserHandler parent) {
38          super(parent);
39      }
40  
41      /**
42       * Sets file's length
43       * @param adpate adapter
44       * @param resource a file
45       * @return response property
46       */
47      public ResponseProperty processSetProperty(ResourceAdapter adapter, Object resource) {
48          long newLength = getContentLength();
49          if (newLength >= 0) {
50              Resource r = (Resource)resource;
51              try {
52                  File file = r.getFile();
53                  RandomAccessFile raf = new RandomAccessFile(file, "rw");
54                  long oldLength = raf.length();
55                  if (newLength != oldLength) {
56                      raf.setLength(newLength);
57                      File f = new File(file.getParentFile(), file.getName());
58                      if (newLength == f.length()) {
59                          return processResponse(adapter, file);
60                      }
61                  }
62              } catch (IOException e) {
63  
64              }
65              return constructResponse(Status.FORBIDDEN);
66          } else {
67              return super.processSetProperty(adapter, resource);
68          }
69      }
70  }
71