View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.jcr;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.net.URL;
21  import java.net.URLConnection;
22  import java.net.URLStreamHandler;
23  
24  import javax.jcr.Item;
25  import javax.jcr.Node;
26  import javax.jcr.Property;
27  import javax.jcr.PropertyType;
28  import javax.jcr.RepositoryException;
29  import javax.jcr.Session;
30  import javax.jcr.nodetype.NodeType;
31  
32  /** URL stream handler able to deal with nt:file node and properties. NOT FINISHED */
33  public class JcrUrlStreamHandler extends URLStreamHandler {
34  	private final Session session;
35  
36  	public JcrUrlStreamHandler(Session session) {
37  		this.session = session;
38  	}
39  
40  	@Override
41  	protected URLConnection openConnection(final URL u) throws IOException {
42  		// TODO Auto-generated method stub
43  		return new URLConnection(u) {
44  
45  			@Override
46  			public void connect() throws IOException {
47  				String itemPath = u.getPath();
48  				try {
49  					if (!session.itemExists(itemPath))
50  						throw new IOException("No item under " + itemPath);
51  
52  					Item item = session.getItem(u.getPath());
53  					if (item.isNode()) {
54  						// this should be a nt:file node
55  						Node node = (Node) item;
56  						if (!node.getPrimaryNodeType().isNodeType(
57  								NodeType.NT_FILE))
58  							throw new IOException("Node " + node + " is not a "
59  									+ NodeType.NT_FILE);
60  
61  					} else {
62  						Property property = (Property) item;
63  						if(property.getType()==PropertyType.BINARY){
64  							//Binary binary = property.getBinary();
65  							
66  						}
67  					}
68  				} catch (RepositoryException e) {
69  					IOException ioe = new IOException(
70  							"Unexpected JCR exception");
71  					ioe.initCause(e);
72  					throw ioe;
73  				}
74  			}
75  
76  			@Override
77  			public InputStream getInputStream() throws IOException {
78  				// TODO Auto-generated method stub
79  				return super.getInputStream();
80  			}
81  
82  		};
83  	}
84  
85  }