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.apache.jackrabbit.JcrConstants; 030 031import org.ametys.plugins.repository.AmetysRepositoryException; 032import org.ametys.plugins.repository.RepositoryConstants; 033import org.ametys.plugins.repository.metadata.ModifiableResource; 034 035/** 036 * Java binding of a nt:resource JCR Node 037 * @deprecated Use org.ametys.cms.data.Resource instead 038 */ 039@Deprecated 040public class JCRResource implements ModifiableResource 041{ 042 private Node _node; 043 private boolean _lockAlreadyChecked; 044 045 /** 046 * Constructor 047 * @param node the nt:resource Node 048 */ 049 public JCRResource(Node node) 050 { 051 _node = node; 052 } 053 054 /** 055 * Retrieves the underlying node. 056 * @return the underlying node. 057 */ 058 public Node getNode() 059 { 060 return _node; 061 } 062 063 @Override 064 public void rename(String newName) throws AmetysRepositoryException 065 { 066 try 067 { 068 _checkLock(); 069 getNode().getSession().move(getNode().getPath(), getNode().getParent().getPath() + "/" + newName); 070 } 071 catch (RepositoryException e) 072 { 073 throw new AmetysRepositoryException(e); 074 } 075 } 076 077 public String getEncoding() throws AmetysRepositoryException 078 { 079 try 080 { 081 return _node.getProperty(JcrConstants.JCR_ENCODING).getString(); 082 } 083 catch (PathNotFoundException e) 084 { 085 // Not found 086 return null; 087 } 088 catch (RepositoryException e) 089 { 090 throw new AmetysRepositoryException(e); 091 } 092 } 093 094 public InputStream getInputStream() throws AmetysRepositoryException 095 { 096 try 097 { 098 return _node.getProperty(JcrConstants.JCR_DATA).getBinary().getStream(); 099 } 100 catch (RepositoryException e) 101 { 102 throw new AmetysRepositoryException(e); 103 } 104 } 105 106 public Date getLastModified() throws AmetysRepositoryException 107 { 108 try 109 { 110 return _node.getProperty(JcrConstants.JCR_LASTMODIFIED).getDate().getTime(); 111 } 112 catch (RepositoryException e) 113 { 114 throw new AmetysRepositoryException(e); 115 } 116 } 117 118 public long getLength() throws AmetysRepositoryException 119 { 120 try 121 { 122 return _node.getProperty(JcrConstants.JCR_DATA).getLength(); 123 } 124 catch (RepositoryException e) 125 { 126 throw new AmetysRepositoryException(e); 127 } 128 } 129 130 public String getMimeType() throws AmetysRepositoryException 131 { 132 try 133 { 134 return _node.getProperty(JcrConstants.JCR_MIMETYPE).getString(); 135 } 136 137 catch (RepositoryException e) 138 { 139 throw new AmetysRepositoryException(e); 140 } 141 } 142 143 public void setEncoding(String encoding) throws AmetysRepositoryException 144 { 145 try 146 { 147 _checkLock(); 148 _node.setProperty(JcrConstants.JCR_ENCODING, encoding); 149 } 150 catch (RepositoryException e) 151 { 152 throw new AmetysRepositoryException(e); 153 } 154 } 155 156 public void setInputStream(InputStream stream) throws AmetysRepositoryException 157 { 158 try 159 { 160 _checkLock(); 161 Binary binary = _node.getSession().getValueFactory().createBinary(stream); 162 _node.setProperty(JcrConstants.JCR_DATA, binary); 163 } 164 catch (RepositoryException e) 165 { 166 throw new AmetysRepositoryException(e); 167 } 168 } 169 170 public void setLastModified(Date lastModifiedDate) throws AmetysRepositoryException 171 { 172 GregorianCalendar gc = new GregorianCalendar(); 173 gc.setTime(lastModifiedDate); 174 175 try 176 { 177 _checkLock(); 178 _node.setProperty(JcrConstants.JCR_LASTMODIFIED, gc); 179 } 180 catch (RepositoryException e) 181 { 182 throw new AmetysRepositoryException(e); 183 } 184 } 185 186 public void setMimeType(String mimeType) throws AmetysRepositoryException 187 { 188 try 189 { 190 _checkLock(); 191 _node.setProperty(JcrConstants.JCR_MIMETYPE, mimeType); 192 } 193 catch (RepositoryException e) 194 { 195 throw new AmetysRepositoryException(e); 196 } 197 } 198 199 public OutputStream getOutputStream() throws AmetysRepositoryException 200 { 201 return new JCROutputStream(_node, false); 202 } 203 204 /** 205 * Check if the node is locked 206 * @throws RepositoryException if an error occurred 207 */ 208 protected void _checkLock() throws RepositoryException 209 { 210 if (!_lockAlreadyChecked && _node.isLocked()) 211 { 212 Lock lock = _node.getSession().getWorkspace().getLockManager().getLock(_node.getPath()); 213 Node lockHolder = lock.getNode(); 214 215 lockHolder.getSession().getWorkspace().getLockManager().addLockToken(lockHolder.getProperty(RepositoryConstants.METADATA_LOCKTOKEN).getString()); 216 _lockAlreadyChecked = true; 217 } 218 } 219}