View Javadoc

1   package com.bradmcevoy.http.webdav;
2   
3   import com.bradmcevoy.io.StreamUtils;
4   import java.io.ByteArrayInputStream;
5   import org.apache.commons.io.output.ByteArrayOutputStream;
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.util.LinkedHashSet;
9   import java.util.Set;
10  import javax.xml.namespace.QName;
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  import org.xml.sax.InputSource;
14  import org.xml.sax.SAXException;
15  import org.xml.sax.XMLReader;
16  import org.xml.sax.helpers.XMLReaderFactory;
17  
18  /**
19   * Simple implmentation which just parses the request body. If no xml is present
20   * it will return an empty set.
21   *
22   * Note this generally shouldnt be used directly, but should be wrapped by
23   * MSPropFindRequestFieldParser to support windows clients.
24   *
25   * @author brad
26   */
27  public class DefaultPropFindRequestFieldParser implements PropFindRequestFieldParser {
28  
29      private static final Logger log = LoggerFactory.getLogger( DefaultPropFindRequestFieldParser.class );
30  
31      public DefaultPropFindRequestFieldParser() {
32      }
33  
34      public ParseResult getRequestedFields( InputStream in ) {
35          try {
36              final Set<QName> set = new LinkedHashSet<QName>();
37              ByteArrayOutputStream bout = new ByteArrayOutputStream();
38              StreamUtils.readTo( in, bout, false, true );
39              byte[] arr = bout.toByteArray();
40              if( arr.length > 1 ) {
41                  ByteArrayInputStream bin = new ByteArrayInputStream( arr );
42                  XMLReader reader = XMLReaderFactory.createXMLReader();
43                  PropFindSaxHandler handler = new PropFindSaxHandler();
44                  reader.setContentHandler( handler );
45                  try {
46                      reader.parse( new InputSource( bin ) );
47                      if( handler.isAllProp() ) {
48                          return new ParseResult( true, set );
49                      } else {
50                          set.addAll( handler.getAttributes().keySet() );
51                      }
52                  } catch( IOException e ) {
53                      log.warn( "exception parsing request body", e );
54                      // ignore
55                  } catch( SAXException e ) {
56                      log.warn( "exception parsing request body", e );
57                      // ignore
58                  }
59              }
60              return new ParseResult( false, set );
61          } catch( Exception ex ) {
62              throw new RuntimeException( ex );
63          }
64      }
65  }