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.fs.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  /**
25   * This property can set file's length.
26   *
27   * @author Daniel Sendula
28   */
29  public class GetContentLength extends org.abstracthorizon.danube.webdav.xml.dav.request.properties.GetContentLength {
30  
31      /**
32       * Constructor
33       * @param parent
34       */
35      public GetContentLength(XMLParserHandler parent) {
36          super(parent);
37      }
38  
39      /**
40       * Sets file's length
41       * @param adpate adapter
42       * @param resource a file
43       * @return response property
44       */
45      public ResponseProperty processSetProperty(ResourceAdapter adapter, Object resource) {
46          long newLength = getContentLength();
47          if (newLength >= 0) {
48              File file = (File)resource;
49              try {
50                  RandomAccessFile raf = new RandomAccessFile(file, "rw");
51                  long oldLength = raf.length();
52                  if (newLength != oldLength) {
53                      raf.setLength(newLength);
54                      File f = new File(file.getParentFile(), file.getName());
55                      if (newLength == f.length()) {
56                          return processResponse(adapter, file);
57                      }
58                  }
59              } catch (IOException e) {
60  
61              }
62              return constructResponse(Status.FORBIDDEN);
63          } else {
64              return super.processSetProperty(adapter, resource);
65          }
66      }
67  }
68