001/* 002 * Copyright 2019 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.cms.data; 017 018import java.io.InputStream; 019import java.time.ZoneId; 020import java.time.ZonedDateTime; 021 022import javax.jcr.RepositoryException; 023import javax.jcr.Session; 024 025import org.ametys.core.util.DateUtils; 026import org.ametys.plugins.explorer.resources.ModifiableResource; 027import org.ametys.plugins.repository.AmetysObjectResolver; 028import org.ametys.plugins.repository.AmetysRepositoryException; 029import org.ametys.plugins.repository.UnknownAmetysObjectException; 030 031/** 032 * Class representing a file from explorer 033 */ 034public class ExplorerFile implements File 035{ 036 private AmetysObjectResolver _resolver; 037 private String _resourceId; 038 private ModifiableResource _resource; 039 private Session _session; 040 041 /** 042 * Constructor of the explorer file 043 * @param resolver resolver used to get the resource from its identifier 044 * @param resourceId resource's identifier 045 */ 046 public ExplorerFile(AmetysObjectResolver resolver, String resourceId) 047 { 048 this(resolver, resourceId, null); 049 } 050 051 /** 052 * Constructor of the explorer file 053 * @param resolver resolver used to get the resource from its identifier 054 * @param resourceId resource's identifier 055 * @param session the current session. If <code>null</code>, a new session will be used to retrieve the resource's data 056 */ 057 public ExplorerFile(AmetysObjectResolver resolver, String resourceId, Session session) 058 { 059 _resolver = resolver; 060 _resourceId = resourceId; 061 _session = session; 062 } 063 064 /** 065 * Retrieves the resource's identifier 066 * @return the resource's identifier 067 */ 068 public String getResourceId() 069 { 070 return _resourceId; 071 } 072 073 public InputStream getInputStream() 074 { 075 return _getResource().getInputStream(); 076 } 077 078 public ZonedDateTime getLastModificationDate() 079 { 080 return DateUtils.asZonedDateTime(_getResource().getLastModified(), ZoneId.systemDefault()); 081 } 082 083 public void setLastModificationDate(ZonedDateTime lastModificationDate) 084 { 085 _getResource().setLastModified(DateUtils.asDate(lastModificationDate)); 086 } 087 088 public long getLength() 089 { 090 return _getResource().getLength(); 091 } 092 093 public String getMimeType() 094 { 095 return _getResource().getMimeType(); 096 } 097 098 public void setMimeType(String mimeType) 099 { 100 _getResource().setMimeType(mimeType); 101 } 102 103 public String getName() 104 { 105 return _getResource().getName(); 106 } 107 108 public String getPath() 109 { 110 return _getResource().getId(); 111 } 112 113 private ModifiableResource _getResource() throws AmetysRepositoryException, UnknownAmetysObjectException 114 { 115 if (_resource == null) 116 { 117 if (_session != null) 118 { 119 try 120 { 121 _resource = _resolver.resolveById(_resourceId, _session); 122 } 123 catch (RepositoryException e) 124 { 125 throw new AmetysRepositoryException("Unable to retrieve the resource with the id '" + _resourceId + "'.", e); 126 } 127 } 128 else 129 { 130 _resource = _resolver.resolveById(_resourceId); 131 } 132 } 133 134 return _resource; 135 } 136}