1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
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  		
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  						
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  							
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  				
79  				return super.getInputStream();
80  			}
81  
82  		};
83  	}
84  
85  }