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.xml.dav.request.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.request.AbstractSimpleXMLHandler;
19  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.ResponseProperty;
20  
21  import java.lang.reflect.Constructor;
22  import java.lang.reflect.InvocationTargetException;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * Abstract request property class. It defines default behaviour of undefined properties.
29   *
30   * @author Daniel Sendula
31   */
32  public abstract class RequestProperty extends AbstractSimpleXMLHandler {
33  
34      /** Logger */
35      protected Logger logger = LoggerFactory.getLogger(getClass());
36  
37      /** Constructor argument classes - {@link Status} */
38      protected static final Class<?>[] CONSTRUCTOR_ARGUMENTS_CLASSES = new Class[]{Status.class};
39  
40      /** This package name */
41      protected static final String packageName = ResponseProperty.class.getPackage().getName();
42  
43      /**
44       * Constructor
45       * @param parent parent parser handler
46       */
47      public RequestProperty(XMLParserHandler parent) {
48          super(parent);
49      }
50  
51      /**
52       * Constructs response property based on supplied status
53       * @param status status
54       * @return response property from this package
55       */
56      @SuppressWarnings("unchecked")
57      protected ResponseProperty constructResponse(Status status) {
58          try {
59              String clsName = getClass().getName();
60              int i = clsName.lastIndexOf('.');
61              clsName = packageName + '.' + clsName.substring(i + 1);
62              Class cls = Class.forName(clsName);
63              Constructor<ResponseProperty> constructor = cls.getConstructor(CONSTRUCTOR_ARGUMENTS_CLASSES);
64              ResponseProperty responseProperty = constructor.newInstance(new Object[]{status});
65              return responseProperty;
66          } catch (SecurityException e) {
67              logger.debug("", e);
68          } catch (NoSuchMethodException e) {
69              logger.debug("", e);
70          } catch (IllegalArgumentException e) {
71              logger.debug("", e);
72          } catch (InstantiationException e) {
73              logger.debug("", e);
74          } catch (IllegalAccessException e) {
75              logger.debug("", e);
76          } catch (InvocationTargetException e) {
77              logger.debug("", e);
78          } catch (ClassNotFoundException e) {
79              logger.debug("", e);
80          }
81          return null;
82      }
83  
84      /**
85       * Returns response property with {@link Status#NOT_FOUND} status
86       * @param adapter adpater
87       * @param resource resource
88       * @return response property with {@link Status#NOT_FOUND} status
89       */
90      public ResponseProperty processResponse(ResourceAdapter adapter, Object resource) {
91          return constructResponse(Status.NOT_FOUND);
92      }
93  
94      /**
95       * Returns response property with {@link Status#CONFLICT} status
96       * @param adapter adpater
97       * @param resource resource
98       * @return response property with {@link Status#CONFLICT} status
99       */
100     public ResponseProperty processSetProperty(ResourceAdapter adapter, Object resource) {
101         return constructResponse(Status.CONFLICT);
102     }
103 
104     /**
105      * Returns response property with {@link Status#CONFLICT} status
106      * @param adapter adpater
107      * @param resource resource
108      * @return response property with {@link Status#CONFLICT} status
109      */
110     public ResponseProperty processRemoveProperty(ResourceAdapter webDAVAdapter, Object resource) {
111         return constructResponse(Status.CONFLICT);
112     }
113 
114 }