001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.repository.metadata.jcr; 017 018import java.io.InputStream; 019import java.io.OutputStream; 020import java.util.Date; 021import java.util.GregorianCalendar; 022 023import javax.jcr.Binary; 024import javax.jcr.Node; 025import javax.jcr.PathNotFoundException; 026import javax.jcr.RepositoryException; 027import javax.jcr.lock.Lock; 028 029import org.ametys.plugins.repository.AmetysRepositoryException; 030import org.ametys.plugins.repository.RepositoryConstants; 031import org.ametys.plugins.repository.metadata.ModifiableResource; 032 033/** 034 * Java binding of a nt:resource JCR Node 035 */ 036public class JCRResource implements ModifiableResource 037{ 038 private Node _node; 039 private boolean _lockAlreadyChecked; 040 041 /** 042 * Constructor 043 * @param node the nt:resource Node 044 */ 045 public JCRResource(Node node) 046 { 047 _node = node; 048 } 049 050 /** 051 * Retrieves the underlying node. 052 * @return the underlying node. 053 */ 054 public Node getNode() 055 { 056 return _node; 057 } 058 059 @Override 060 public void rename(String newName) throws AmetysRepositoryException 061 { 062 try 063 { 064 _checkLock(); 065 getNode().getSession().move(getNode().getPath(), getNode().getParent().getPath() + "/" + newName); 066 } 067 catch (RepositoryException e) 068 { 069 throw new AmetysRepositoryException(e); 070 } 071 } 072 073 public String getEncoding() throws AmetysRepositoryException 074 { 075 try 076 { 077 return _node.getProperty("jcr:encoding").getString(); 078 } 079 catch (PathNotFoundException e) 080 { 081 // Not found 082 return null; 083 } 084 catch (RepositoryException e) 085 { 086 throw new AmetysRepositoryException(e); 087 } 088 } 089 090 public InputStream getInputStream() throws AmetysRepositoryException 091 { 092 try 093 { 094 return _node.getProperty("jcr:data").getBinary().getStream(); 095 } 096 catch (RepositoryException e) 097 { 098 throw new AmetysRepositoryException(e); 099 } 100 } 101 102 public Date getLastModified() throws AmetysRepositoryException 103 { 104 try 105 { 106 return _node.getProperty("jcr:lastModified").getDate().getTime(); 107 } 108 catch (RepositoryException e) 109 { 110 throw new AmetysRepositoryException(e); 111 } 112 } 113 114 public long getLength() throws AmetysRepositoryException 115 { 116 try 117 { 118 return _node.getProperty("jcr:data").getLength(); 119 } 120 catch (RepositoryException e) 121 { 122 throw new AmetysRepositoryException(e); 123 } 124 } 125 126 public String getMimeType() throws AmetysRepositoryException 127 { 128 try 129 { 130 return _node.getProperty("jcr:mimeType").getString(); 131 } 132 133 catch (RepositoryException e) 134 { 135 throw new AmetysRepositoryException(e); 136 } 137 } 138 139 public void setEncoding(String encoding) throws AmetysRepositoryException 140 { 141 try 142 { 143 _checkLock(); 144 _node.setProperty("jcr:encoding", encoding); 145 } 146 catch (RepositoryException e) 147 { 148 throw new AmetysRepositoryException(e); 149 } 150 } 151 152 public void setInputStream(InputStream stream) throws AmetysRepositoryException 153 { 154 try 155 { 156 _checkLock(); 157 Binary binary = _node.getSession().getValueFactory().createBinary(stream); 158 _node.setProperty("jcr:data", binary); 159 } 160 catch (RepositoryException e) 161 { 162 throw new AmetysRepositoryException(e); 163 } 164 } 165 166 public void setLastModified(Date lastModifiedDate) throws AmetysRepositoryException 167 { 168 GregorianCalendar gc = new GregorianCalendar(); 169 gc.setTime(lastModifiedDate); 170 171 try 172 { 173 _checkLock(); 174 _node.setProperty("jcr:lastModified", gc); 175 } 176 catch (RepositoryException e) 177 { 178 throw new AmetysRepositoryException(e); 179 } 180 } 181 182 public void setMimeType(String mimeType) throws AmetysRepositoryException 183 { 184 try 185 { 186 _checkLock(); 187 _node.setProperty("jcr:mimeType", mimeType); 188 } 189 catch (RepositoryException e) 190 { 191 throw new AmetysRepositoryException(e); 192 } 193 } 194 195 public OutputStream getOutputStream() throws AmetysRepositoryException 196 { 197 return new JCROutputStream(_node); 198 } 199 200 private void _checkLock() throws RepositoryException 201 { 202 if (!_lockAlreadyChecked && _node.isLocked()) 203 { 204 Lock lock = _node.getSession().getWorkspace().getLockManager().getLock(_node.getPath()); 205 Node lockHolder = lock.getNode(); 206 207 lockHolder.getSession().getWorkspace().getLockManager().addLockToken(lockHolder.getProperty(RepositoryConstants.METADATA_LOCKTOKEN).getString()); 208 _lockAlreadyChecked = true; 209 } 210 } 211}