/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License, version 2 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/gpl-2.0.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
*
* Copyright 2006 - 2013 Pentaho Corporation. All rights reserved.
*/
package org.pentaho.platform.repository2.unified.webservices.jaxws;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import org.pentaho.platform.api.locale.IPentahoLocale;
import org.pentaho.platform.api.repository2.unified.IRepositoryFileData;
import org.pentaho.platform.api.repository2.unified.IUnifiedRepository;
import org.pentaho.platform.api.repository2.unified.RepositoryFile;
import org.pentaho.platform.api.repository2.unified.RepositoryFileAce;
import org.pentaho.platform.api.repository2.unified.RepositoryFileAcl;
import org.pentaho.platform.api.repository2.unified.RepositoryFilePermission;
import org.pentaho.platform.api.repository2.unified.RepositoryFileTree;
import org.pentaho.platform.api.repository2.unified.RepositoryRequest;
import org.pentaho.platform.api.repository2.unified.VersionSummary;
import org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData;
import org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData;
import org.pentaho.platform.repository2.locale.PentahoLocale;
import org.pentaho.platform.repository2.unified.webservices.IUnifiedRepositoryWebService;
import org.pentaho.platform.repository2.unified.webservices.NodeRepositoryFileDataAdapter;
import org.pentaho.platform.repository2.unified.webservices.NodeRepositoryFileDataDto;
import org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclAceAdapter;
import org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclAceDto;
import org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclAdapter;
import org.pentaho.platform.repository2.unified.webservices.RepositoryFileAdapter;
import org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto;
import org.pentaho.platform.repository2.unified.webservices.RepositoryFileTreeAdapter;
import org.pentaho.platform.repository2.unified.webservices.StringKeyStringValueDto;
import org.pentaho.platform.repository2.unified.webservices.VersionSummaryAdapter;
import org.pentaho.platform.repository2.unified.webservices.VersionSummaryDto;
import org.springframework.util.Assert;
/**
* Converts calls to {@link IUnifiedRepository} into {@link IUnifiedRepositoryWebService}. This is how client code
* remains unaware of server code location.
*
* @author mlowery
*/
public class UnifiedRepositoryToWebServiceAdapter implements IUnifiedRepository {
private volatile List<Character> cachedReservedChars; // make sure threads see up-to-date value
private IUnifiedRepositoryJaxwsWebService repoWebService;
private RepositoryFileAdapter repositoryFileAdapter = new RepositoryFileAdapter();
private RepositoryFileTreeAdapter repositoryFileTreeAdapter = new RepositoryFileTreeAdapter();
private NodeRepositoryFileDataAdapter nodeRepositoryFileDataAdapter = new NodeRepositoryFileDataAdapter();
private RepositoryFileAclAdapter repositoryFileAclAdapter = new RepositoryFileAclAdapter();
private RepositoryFileAclAceAdapter repositoryFileAclAceAdapter = new RepositoryFileAclAceAdapter();
private VersionSummaryAdapter versionSummaryAdapter = new VersionSummaryAdapter();
public UnifiedRepositoryToWebServiceAdapter( IUnifiedRepositoryJaxwsWebService repoWebService ) {
super();
this.repoWebService = repoWebService;
}
@Override
public RepositoryFile createFile( Serializable parentFolderId, RepositoryFile file, IRepositoryFileData data,
String versionMessage ) {
if ( data instanceof NodeRepositoryFileData ) {
return repositoryFileAdapter.unmarshal( repoWebService.createFile( parentFolderId != null ? parentFolderId
.toString() : null, repositoryFileAdapter.marshal( file ), nodeRepositoryFileDataAdapter
.marshal( (NodeRepositoryFileData) data ), versionMessage ) );
} else if ( data instanceof SimpleRepositoryFileData ) {
SimpleRepositoryFileData simpleData = (SimpleRepositoryFileData) data;
return repositoryFileAdapter.unmarshal( repoWebService.createBinaryFile( parentFolderId != null ? parentFolderId
.toString() : null, repositoryFileAdapter.marshal( file ), SimpleRepositoryFileDataDto.convert( simpleData ),
versionMessage ) );
} else {
throw new IllegalArgumentException();
}
}
@Override
public RepositoryFile createFolder( Serializable parentFolderId, RepositoryFile file, String versionMessage ) {
return repositoryFileAdapter.unmarshal( repoWebService.createFolder( parentFolderId != null ? parentFolderId
.toString() : null, repositoryFileAdapter.marshal( file ), versionMessage ) );
}
@Override
public void deleteFile( Serializable fileId, boolean permanent, String versionMessage ) {
repoWebService.deleteFileWithPermanentFlag( fileId != null ? fileId.toString() : null, permanent, versionMessage );
}
@Override
public void deleteFile( Serializable fileId, String versionMessage ) {
repoWebService.deleteFile( fileId != null ? fileId.toString() : null, versionMessage );
}
@Override
public void deleteFileAtVersion( Serializable fileId, Serializable versionId ) {
repoWebService.deleteFileAtVersion( fileId != null ? fileId.toString() : null, versionId.toString() != null
? versionId.toString() : null );
}
@Override
public RepositoryFileAcl getAcl( Serializable fileId ) {
return repositoryFileAclAdapter.unmarshal( repoWebService.getAcl( fileId != null ? fileId.toString() : null ) );
}
@Override
public List<RepositoryFile> getChildren( RepositoryRequest repositoryRequest ) {
return unmarshalFiles( repoWebService.getChildrenFromRequest( repositoryRequest ) );
}
@Override
@Deprecated
public List<RepositoryFile> getChildren( Serializable folderId ) {
return unmarshalFiles( repoWebService.getChildren( folderId.toString() != null ? folderId.toString() : null ) );
}
@Override
@Deprecated
public List<RepositoryFile> getChildren( Serializable folderId, String filter ) {
return unmarshalFiles( repoWebService.getChildrenWithFilter( folderId.toString() != null ? folderId.toString()
: null, filter ) );
}
private List<RepositoryFile> unmarshalFiles( List<RepositoryFileDto> dtos ) {
List<RepositoryFile> files = new ArrayList<RepositoryFile>();
for ( RepositoryFileDto dto : dtos ) {
files.add( repositoryFileAdapter.unmarshal( dto ) );
}
return files;
}
@Override
public List<RepositoryFile> getChildren( Serializable folderId, String filter, Boolean showHiddenFiles ) {
return unmarshalFiles( repoWebService.getChildrenWithFilterAndHidden( folderId.toString() != null ? folderId
.toString() : null, filter, showHiddenFiles ) );
}
@Override
public <T extends IRepositoryFileData> T getDataForExecute( Serializable fileId, Class<T> dataClass ) {
throw new UnsupportedOperationException();
}
@Override
public <T extends IRepositoryFileData> List<T> getDataForExecuteInBatch( final List<RepositoryFile> files,
Class<T> dataClass ) {
throw new UnsupportedOperationException();
}
@Override
public <T extends IRepositoryFileData> T getDataAtVersionForExecute( Serializable fileId, Serializable versionId,
Class<T> dataClass ) {
throw new UnsupportedOperationException();
}
@Override
@SuppressWarnings( "unchecked" )
public <T extends IRepositoryFileData> T getDataForRead( Serializable fileId, Class<T> dataClass ) {
if ( dataClass.equals( NodeRepositoryFileData.class ) ) {
return (T) nodeRepositoryFileDataAdapter.unmarshal( repoWebService.getDataAsNodeForRead( fileId != null ? fileId
.toString() : null ) );
} else if ( dataClass.equals( SimpleRepositoryFileData.class ) ) {
SimpleRepositoryFileDataDto simpleJaxWsData =
repoWebService.getDataAsBinaryForRead( fileId != null ? fileId.toString() : null );
return (T) SimpleRepositoryFileDataDto.convert( simpleJaxWsData );
} else {
throw new IllegalArgumentException();
}
}
@Override
@SuppressWarnings( "unchecked" )
public <T extends IRepositoryFileData> java.util.List<T> getDataForReadInBatch( final List<RepositoryFile> files,
final Class<T> dataClass ) {
List<RepositoryFileDto> fileDtos = new ArrayList<RepositoryFileDto>( files.size() );
for ( RepositoryFile file : files ) {
fileDtos.add( repositoryFileAdapter.marshal( file ) );
}
if ( dataClass.equals( NodeRepositoryFileData.class ) ) {
List<NodeRepositoryFileDataDto> nodeData = repoWebService.getDataAsNodeForReadInBatch( fileDtos );
List<T> data = new ArrayList<T>( nodeData.size() );
for ( NodeRepositoryFileDataDto node : nodeData ) {
data.add( (T) nodeRepositoryFileDataAdapter.unmarshal( node ) );
}
return data;
} else if ( dataClass.equals( SimpleRepositoryFileData.class ) ) {
List<SimpleRepositoryFileDataDto> nodeData = repoWebService.getDataAsBinaryForReadInBatch( fileDtos );
List<T> data = new ArrayList<T>( nodeData.size() );
for ( SimpleRepositoryFileDataDto node : nodeData ) {
data.add( (T) SimpleRepositoryFileDataDto.convert( node ) );
}
return data;
} else {
throw new IllegalArgumentException();
}
}
@Override
@SuppressWarnings( "unchecked" )
public <T extends IRepositoryFileData> T getDataAtVersionForRead( Serializable fileId, Serializable versionId,
Class<T> dataClass ) {
if ( dataClass.equals( NodeRepositoryFileData.class ) ) {
return (T) nodeRepositoryFileDataAdapter.unmarshal( repoWebService.getDataAsNodeForReadAtVersion( fileId != null
? fileId.toString() : null, versionId != null ? versionId.toString() : null ) );
} else if ( dataClass.equals( SimpleRepositoryFileData.class ) ) {
SimpleRepositoryFileDataDto simpleJaxWsData =
repoWebService.getDataAsBinaryForReadAtVersion( fileId != null ? fileId.toString() : null, versionId != null
? versionId.toString() : null );
return (T) SimpleRepositoryFileDataDto.convert( simpleJaxWsData );
} else {
throw new IllegalArgumentException();
}
}
@Override
public List<RepositoryFile> getDeletedFiles( final String origParentFolderPath ) {
return unmarshalFiles( repoWebService.getDeletedFilesInFolder( origParentFolderPath ) );
}
@Override
public List<RepositoryFile> getDeletedFiles( final String origParentFolderPath, String filter ) {
return unmarshalFiles( repoWebService.getDeletedFilesInFolderWithFilter( origParentFolderPath, filter ) );
}
@Override
public List<RepositoryFile> getDeletedFiles() {
return unmarshalFiles( repoWebService.getDeletedFiles() );
}
@Override
public List<RepositoryFileAce> getEffectiveAces( Serializable fileId ) {
return unmarshalAces( repoWebService.getEffectiveAces( fileId != null ? fileId.toString() : null ) );
}
private List<RepositoryFileAce> unmarshalAces( List<RepositoryFileAclAceDto> dtos ) {
List<RepositoryFileAce> aces = new ArrayList<RepositoryFileAce>();
for ( RepositoryFileAclAceDto dto : dtos ) {
aces.add( repositoryFileAclAceAdapter.unmarshal( dto ) );
}
return aces;
}
@Override
public List<RepositoryFileAce> getEffectiveAces( Serializable fileId, boolean forceEntriesInheriting ) {
return unmarshalAces( repoWebService.getEffectiveAcesWithForceFlag( fileId != null ? fileId.toString() : null,
forceEntriesInheriting ) );
}
@Override
public RepositoryFile getFile( String path ) {
// path = path.replaceAll( ";", "/" ); //Why is it here?
return repositoryFileAdapter.unmarshal( repoWebService.getFile( path, false, null ) );
}
@Override
public RepositoryFile getFileAtVersion( Serializable fileId, Serializable versionId ) {
return repositoryFileAdapter.unmarshal( repoWebService.getFileAtVersion( fileId != null ? fileId.toString() : null,
versionId != null ? versionId.toString() : null ) );
}
@Override
public RepositoryFile getFileById( Serializable fileId ) {
return repositoryFileAdapter.unmarshal( repoWebService.getFileById( fileId != null ? fileId.toString() : null,
false, null ) );
}
@Override
public RepositoryFile getFile( String path, boolean loadLocaleMaps ) {
return this.repositoryFileAdapter.unmarshal( this.repoWebService.getFile( path, loadLocaleMaps, null ) );
}
@Override
public RepositoryFile getFileById( Serializable fileId, boolean loadLocaleMaps ) {
return this.repositoryFileAdapter.unmarshal( this.repoWebService.getFileById( fileId != null ? fileId.toString()
: null, loadLocaleMaps, null ) );
}
@Override
public RepositoryFile getFile( String path, IPentahoLocale locale ) {
return this.repositoryFileAdapter.unmarshal( this.repoWebService.getFile( path, false, (PentahoLocale) locale ) );
}
@Override
public RepositoryFile getFileById( Serializable fileId, IPentahoLocale locale ) {
return this.repositoryFileAdapter.unmarshal( this.repoWebService.getFileById( fileId != null ? fileId.toString()
: null, false, (PentahoLocale) locale ) );
}
@Override
public RepositoryFile getFile( String path, boolean loadLocaleMaps, IPentahoLocale locale ) {
return this.repositoryFileAdapter.unmarshal( this.repoWebService.getFile( path, loadLocaleMaps,
(PentahoLocale) locale ) );
}
@Override
public RepositoryFile getFileById( Serializable fileId, boolean loadLocaleMaps, IPentahoLocale locale ) {
return this.repositoryFileAdapter.unmarshal( this.repoWebService.getFileById( fileId != null ? fileId.toString()
: null, loadLocaleMaps, (PentahoLocale) locale ) );
}
@Override
public List<VersionSummary> getVersionSummaries( Serializable fileId ) {
return unmarshalVersionSummaries( repoWebService.getVersionSummaries( fileId != null ? fileId.toString() : null ) );
}
private List<VersionSummary> unmarshalVersionSummaries( List<VersionSummaryDto> dtos ) {
List<VersionSummary> versionSummaries = new ArrayList<VersionSummary>();
for ( VersionSummaryDto dto : dtos ) {
versionSummaries.add( versionSummaryAdapter.unmarshal( dto ) );
}
return versionSummaries;
}
@Override
public VersionSummary getVersionSummary( Serializable fileId, Serializable versionId ) {
return versionSummaryAdapter.unmarshal( repoWebService.getVersionSummary(
fileId != null ? fileId.toString() : null, versionId != null ? versionId.toString() : null ) );
}
@Override
public List<VersionSummary> getVersionSummaryInBatch( final List<RepositoryFile> files ) {
List<RepositoryFileDto> fileDtos = new ArrayList<RepositoryFileDto>( files.size() );
for ( RepositoryFile file : files ) {
fileDtos.add( repositoryFileAdapter.marshal( file ) );
}
return unmarshalVersionSummaries( repoWebService.getVersionSummaryInBatch( fileDtos ) );
}
@Override
public boolean hasAccess( String path, EnumSet<RepositoryFilePermission> permissions ) {
return repoWebService.hasAccess( path, RepositoryFileAclAceAdapter.toIntPerms( permissions ) );
}
@Override
public void lockFile( Serializable fileId, String message ) {
repoWebService.lockFile( fileId != null ? fileId.toString() : null, message );
}
@Override
public void moveFile( Serializable fileId, String destAbsPath, String versionMessage ) {
repoWebService.moveFile( fileId != null ? fileId.toString() : null, destAbsPath, versionMessage );
}
@Override
public void copyFile( Serializable fileId, String destAbsPath, String versionMessage ) {
repoWebService.copyFile( fileId != null ? fileId.toString() : null, destAbsPath, versionMessage );
}
@Override
public void undeleteFile( Serializable fileId, String versionMessage ) {
repoWebService.undeleteFile( fileId != null ? fileId.toString() : null, versionMessage );
}
@Override
public void unlockFile( Serializable fileId ) {
repoWebService.unlockFile( fileId != null ? fileId.toString() : null );
}
@Override
public RepositoryFileAcl updateAcl( RepositoryFileAcl acl ) {
return repositoryFileAclAdapter.unmarshal( repoWebService.updateAcl( repositoryFileAclAdapter.marshal( acl ) ) );
}
@Override
public RepositoryFile updateFile( RepositoryFile file, IRepositoryFileData data, String versionMessage ) {
if ( data instanceof NodeRepositoryFileData ) {
return repositoryFileAdapter.unmarshal( repoWebService.updateFile( repositoryFileAdapter.marshal( file ),
nodeRepositoryFileDataAdapter.marshal( (NodeRepositoryFileData) data ), versionMessage ) );
} else if ( data instanceof SimpleRepositoryFileData ) {
SimpleRepositoryFileData simpleData = (SimpleRepositoryFileData) data;
return repositoryFileAdapter.unmarshal( repoWebService.updateBinaryFile( repositoryFileAdapter.marshal( file ),
SimpleRepositoryFileDataDto.convert( simpleData ), versionMessage ) );
} else {
throw new IllegalArgumentException();
}
}
@Override
public void restoreFileAtVersion( Serializable fileId, Serializable versionId, String versionMessage ) {
repoWebService.restoreFileAtVersion( fileId.toString(), versionId.toString(), versionMessage );
}
@Override
public boolean canUnlockFile( final Serializable fileId ) {
return repoWebService.canUnlockFile( fileId.toString() );
}
@Override
public RepositoryFileTree getTree( RepositoryRequest repositoryRequest ) {
return repositoryFileTreeAdapter.unmarshal( repoWebService.getTreeFromRequest( repositoryRequest ) );
}
@Override
@Deprecated
public RepositoryFileTree getTree( final String path, final int depth, final String filter, final boolean showHidden ) {
return repositoryFileTreeAdapter.unmarshal( repoWebService.getTree( path, depth, filter, showHidden ) );
}
@Override
public RepositoryFile createFile( final Serializable parentFolderId, final RepositoryFile file,
final IRepositoryFileData data, final RepositoryFileAcl acl, final String versionMessage ) {
if ( data instanceof NodeRepositoryFileData ) {
return repositoryFileAdapter.unmarshal( repoWebService.createFileWithAcl( parentFolderId != null ? parentFolderId
.toString() : null, repositoryFileAdapter.marshal( file ), nodeRepositoryFileDataAdapter
.marshal( (NodeRepositoryFileData) data ), repositoryFileAclAdapter.marshal( acl ), versionMessage ) );
} else if ( data instanceof SimpleRepositoryFileData ) {
SimpleRepositoryFileData simpleData = (SimpleRepositoryFileData) data;
return repositoryFileAdapter.unmarshal( repoWebService.createBinaryFileWithAcl( parentFolderId != null
? parentFolderId.toString() : null, repositoryFileAdapter.marshal( file ), SimpleRepositoryFileDataDto
.convert( simpleData ), repositoryFileAclAdapter.marshal( acl ), versionMessage ) );
} else {
throw new IllegalArgumentException();
}
}
@Override
public RepositoryFile createFolder( final Serializable parentFolderId, final RepositoryFile file,
final RepositoryFileAcl acl, final String versionMessage ) {
return repositoryFileAdapter.unmarshal( repoWebService.createFolderWithAcl( parentFolderId != null ? parentFolderId
.toString() : null, repositoryFileAdapter.marshal( file ), repositoryFileAclAdapter.marshal( acl ),
versionMessage ) );
}
@Override
public List<RepositoryFile> getReferrers( Serializable fileId ) {
List<RepositoryFile> fileList = new ArrayList<RepositoryFile>();
for ( RepositoryFileDto fileDto : repoWebService.getReferrers( fileId != null ? fileId.toString() : null ) ) {
fileList.add( repositoryFileAdapter.unmarshal( fileDto ) );
}
return fileList;
}
@Override
public void setFileMetadata( final Serializable fileId, Map<String, Serializable> metadataMap ) {
Assert.notNull( fileId );
Assert.notNull( metadataMap );
List<StringKeyStringValueDto> fileMetadataMap = new ArrayList<StringKeyStringValueDto>( metadataMap.size() );
for ( final String key : metadataMap.keySet() ) {
fileMetadataMap.add( new StringKeyStringValueDto( key, metadataMap.get( key ).toString() ) );
}
repoWebService.setFileMetadata( fileId.toString(), fileMetadataMap );
}
@Override
public Map<String, Serializable> getFileMetadata( final Serializable fileId ) {
final List<StringKeyStringValueDto> fileMetadata = repoWebService.getFileMetadata( fileId.toString() );
Assert.notNull( fileMetadata );
final Map<String, Serializable> repoFileMetadata = new HashMap<String, Serializable>( fileMetadata.size() );
for ( StringKeyStringValueDto entry : fileMetadata ) {
repoFileMetadata.put( entry.getKey(), entry.getValue() );
}
return repoFileMetadata;
}
@Override
public List<Character> getReservedChars() {
// no need for synchronization here as value to be written will always be the same
if ( cachedReservedChars == null ) {
cachedReservedChars = Collections.unmodifiableList( repoWebService.getReservedChars() );
}
return cachedReservedChars;
}
@Override
public List<Locale> getAvailableLocalesForFileById( Serializable fileId ) {
throw new UnsupportedOperationException();
}
@Override
public List<Locale> getAvailableLocalesForFileByPath( String relPath ) {
throw new UnsupportedOperationException();
}
@Override
public List<Locale> getAvailableLocalesForFile( RepositoryFile repositoryFile ) {
throw new UnsupportedOperationException();
}
@Override
public Properties getLocalePropertiesForFileById( Serializable fileId, String locale ) {
throw new UnsupportedOperationException();
}
@Override
public Properties getLocalePropertiesForFileByPath( String relPath, String locale ) {
throw new UnsupportedOperationException();
}
@Override
public Properties getLocalePropertiesForFile( RepositoryFile repositoryFile, String locale ) {
throw new UnsupportedOperationException();
}
@Override
public void setLocalePropertiesForFileById( Serializable fileId, String locale, Properties properties ) {
throw new UnsupportedOperationException();
}
@Override
public void setLocalePropertiesForFileByPath( String relPath, String locale, Properties properties ) {
throw new UnsupportedOperationException();
}
@Override
public void setLocalePropertiesForFile( RepositoryFile repositoryFile, String locale, Properties properties ) {
throw new UnsupportedOperationException();
}
@Override
public void deleteLocalePropertiesForFile( RepositoryFile repositoryFile, String locale ) {
throw new UnsupportedOperationException();
}
@Override
public RepositoryFile updateFolder( RepositoryFile folder, String versionMessage ) {
return repositoryFileAdapter.unmarshal( repoWebService.updateFolder( repositoryFileAdapter.marshal( folder ),
versionMessage ) );
}
}