FileResource extends AbstractJaxRSResource { @PUT @Path ( "{pathId : .+}" ) @Consumes ( { MediaType.WILDCARD } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully created the file." ), @ResponseCode ( code = 403, condition = "Failure to create the file due to permissions, file already exists, or invalid path id." ) } ) public Response createFile( @PathParam ( "pathId" ) String pathId, InputStream fileContents ) { try { checkCorrectExtension( pathId ); fileService.createFile( httpServletRequest.getCharacterEncoding(), pathId, fileContents ); return buildOkResponse(); } catch ( FileService.InvalidNameException e ) { return Response.status( Response.Status.FORBIDDEN ).entity( "containsIllegalCharacters" ).build(); } catch ( Throwable t ) { return buildServerErrorResponse( t ); } } FileResource();  FileResource( HttpServletResponse httpServletResponse ); static String idToPath( String pathId ); @GET @Path( "/backup" ) @StatusCodes( { @ResponseCode( code = 200, condition = "Successfully exported the existing Pentaho System" ), @ResponseCode( code = 403, condition = "User does not have administrative permissions" ), @ResponseCode( code = 500, condition = "Failure to complete the export." ) } ) Response systemBackup( @HeaderParam ( "user-agent" ) String userAgent ); @POST @Path( "/systemRestore" ) @Consumes( MediaType.MULTIPART_FORM_DATA ) @StatusCodes( { @ResponseCode( code = 200, condition = "Successfully imported the Pentaho System" ), @ResponseCode( code = 403, condition = "User does not have administrative permissions" ), @ResponseCode( code = 500, condition = "Failure to complete the import." ) } ) Response systemRestore( @FormDataParam( "fileUpload" ) InputStream fileUpload, @FormDataParam ( "overwriteFile" ) String overwriteFile,
                                 @FormDataParam ( "applyAclSettings" ) String applyAclSettings, @FormDataParam ( "overwriteAclSettings" ) String overwriteAclSettings ); @PUT @Path( "/delete" ) @Consumes( { MediaType.WILDCARD } ) @StatusCodes( { @ResponseCode( code = 200, condition = "Successfully moved file to trash." ), @ResponseCode( code = 500, condition = "Failure move the file to the trash." ) } ) Response doDeleteFiles( String params ); @PUT @Path ( "/deletepermanent" ) @Consumes ( { MediaType.WILDCARD } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully deleted the comma seperated list of fileIds from the system." ), @ResponseCode ( code = 403, condition = "Failure to delete the file due to path not found." ) } ) Response doDeleteFilesPermanent( String params ); @PUT @Path ( "{pathId : .+}/move" ) @Consumes ( { MediaType.WILDCARD } ) @Facet ( name = "Unsupported" ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully moved the file." ), @ResponseCode ( code = 403, condition = "Failure to move the file due to path not found." ), @ResponseCode ( code = 500, condition = "Failure to move the file." ) } ) Response doMove( @PathParam ( "pathId" ) String destPathId, String params ); @PUT @Path( "/restore" ) @Consumes( { MediaType.WILDCARD } ) @Facet( name = "Unsupported" ) @StatusCodes( { @ResponseCode( code = 200, condition = "Successfully restored the file." ), @ResponseCode( code = 307, condition = "Cannot restore in origin folder, can restore in home folder without " + "conflicts" ), @ResponseCode( code = 403, condition = "Failure to Restore the file." ), @ResponseCode( code = 409, condition = "Cannot restore in origin folder, cannot restore in home folder without " + "conflicts" ), } ) Response doRestore( String params,
                             @QueryParam( value = "overwriteMode" ) Integer mode ); @PUT @Path ( "{pathId : .+}" ) @Consumes ( { MediaType.WILDCARD } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully created the file." ), @ResponseCode ( code = 403, condition = "Failure to create the file due to permissions, file already exists, or invalid path id." ) } ) Response createFile( @PathParam ( "pathId" ) String pathId, InputStream fileContents ); @PUT @Path ( "{pathId : .+}/children" ) @Consumes ( { MediaType.TEXT_PLAIN } ) @Facet ( name = "Unsupported" ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully copied the file." ), @ResponseCode ( code = 500, condition = "Failure to Copy file due to exception while getting file with id fileid..." ), } ) Response doCopyFiles( @PathParam ( "pathId" ) String pathId, @QueryParam ( "mode" ) Integer mode,
                               String params ); @GET @Path ( "{pathId : .+}" ) @Produces ( { MediaType.WILDCARD } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully get the file or directory." ), @ResponseCode ( code = 404, condition = "Failed to find the file or resource." ), @ResponseCode ( code = 500, condition = "Failed to open content." ) } ) Response doGetFileOrDir( @PathParam ( "pathId" ) String pathId ); Response doGetDirAsZip( @PathParam ( "pathId" ) String pathId ); Response doGetDirAsZip( RepositoryFile repositoryFile ); @GET @Path ( "{pathId : .+}/parameterizable" ) @Produces ( MediaType.TEXT_PLAIN ) @Facet ( name = "Unsupported" ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully get the file or directory." ), @ResponseCode ( code = 404, condition = "Failed to find the file or resource." ) } ) // have to accept anything for browsers to work String doIsParameterizable( @PathParam ( "pathId" ) String pathId ); @GET @Path ( "{pathId : .+}/download" ) @Produces ( MediaType.WILDCARD ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successful download." ), @ResponseCode ( code = 400, condition = "Usually a bad pathId." ), @ResponseCode ( code = 403, condition = "pathId points at a file the user doesn't have access to." ), @ResponseCode ( code = 404, condition = "File not found." ), @ResponseCode ( code = 500, condition = "Failed to download file for another reason." ) } ) // have to accept anything for browsers to work Response doGetFileOrDirAsDownload( @HeaderParam ( "user-agent" ) String userAgent,
                                            @PathParam ( "pathId" ) String pathId, @QueryParam ( "withManifest" ) String strWithManifest ); @GET @Path ( "{pathId : .+}/inline" ) @Produces ( MediaType.WILDCARD ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved file." ), @ResponseCode ( code = 403, condition = "Failed to retrieve file due to permission problem." ), @ResponseCode ( code = 404, condition = "Failed to retrieve file due because file was not found." ), @ResponseCode ( code = 500, condition = "Failed to download file because of some other error." ) } ) Response doGetFileAsInline( @PathParam ( "pathId" ) String pathId ); @PUT @Path ( "{pathId : .+}/acl" ) @Consumes ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully saved file." ), @ResponseCode ( code = 403, condition = "Failed to save acls due to missing or incorrect properties." ), @ResponseCode ( code = 400, condition = "Failed to save acls due to malformed xml." ), @ResponseCode ( code = 500, condition = "Failed to save acls due to another error." ) } ) Response setFileAcls( @PathParam ( "pathId" ) String pathId, RepositoryFileAclDto acl ); @PUT @Path ( "{pathId : .+}/creator" ) @Consumes ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved file." ), @ResponseCode ( code = 500, condition = "Failed to download file because of some other error." ) } ) @Facet ( name = "Unsupported" ) Response doSetContentCreator( @PathParam ( "pathId" ) String pathId, RepositoryFileDto contentCreator ); @GET @Path ( "{pathId : .+}/locales" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved locale information." ), @ResponseCode ( code = 404, condition = "Failed to retrieve locales because the file was not found." ), @ResponseCode ( code = 500, condition = "Unable to retrieve locales due to some other error." ) } ) List<LocaleMapDto> doGetFileLocales( @PathParam ( "pathId" ) String pathId ); @GET @Path ( "{pathId : .+}/localeProperties" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved locale properties." ), @ResponseCode ( code = 500, condition = "Unable to retrieve locale properties due to some other error." ) } ) List<StringKeyStringValueDto> doGetLocaleProperties( @PathParam ( "pathId" ) String pathId,
                                                              @QueryParam ( "locale" ) String locale ); @PUT @Path ( "{pathId : .+}/localeProperties" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully updated locale properties." ), @ResponseCode ( code = 500, condition = "Unable to update locale properties due to some other error." ) } ) Response doSetLocaleProperties( @PathParam ( "pathId" ) String pathId, @QueryParam ( "locale" ) String locale,
                                         List<StringKeyStringValueDto> properties ); @PUT @Path ( "{pathId : .+}/deleteLocale" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully deleted the locale." ), @ResponseCode ( code = 500, condition = "Unable to delete the locale properties due to some other error." ) } ) Response doDeleteLocale( @PathParam ( "pathId" ) String pathId, @QueryParam ( "locale" ) String locale ); @GET @Path ( "/properties" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the properties of the root directory." ), @ResponseCode ( code = 404, condition = "Unable to retrieve the properties of the root directory due to file not found error." ), @ResponseCode ( code = 500, condition = "Unable to retrieve the properties of the root directory due to some other error." ) } ) RepositoryFileDto doGetRootProperties(); @GET @Path ( "{pathId : .+}/canAccessMap" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the permissions of the file." ), @ResponseCode ( code = 500, condition = "Unable to retrieve the permissions of the file due to some other error." ) } ) List<Setting> doGetCanAccessList( @PathParam ( "pathId" ) String pathId,
                                           @QueryParam ( "permissions" ) String permissions ); @POST @Path ( "/pathsAccessList" ) @Consumes ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the permissions of the given paths." ), @ResponseCode ( code = 500, condition = "Unable to retrieve the permissions of the given paths due to some other error." ) } ) List<Setting> doGetPathsAccessList( StringListWrapper pathsWrapper ); @GET @Path ( "{pathId : .+}/canAccess" ) @Produces ( MediaType.TEXT_PLAIN ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the permissions of the given paths." ), @ResponseCode ( code = 500, condition = "Unable to retrieve the permissions of the given paths due to some other error." ) } ) String doGetCanAccess( @PathParam ( "pathId" ) String pathId,
                                @QueryParam ( "permissions" ) String permissions ); @GET @Path ( "/canAdminister" ) @Produces ( MediaType.TEXT_PLAIN ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully returns a boolean value, either true or false" ) } ) String doGetCanAdminister(); @GET @Path ( "/reservedCharacters" ) @Produces ( { MediaType.TEXT_PLAIN } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully returns a list of repositroy reserved characters" ) } ) Response doGetReservedChars(); @GET @Path ( "/reservedCharactersDisplay" ) @Produces ( { MediaType.TEXT_PLAIN } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully returns a list of repositroy reserved characters" ) } ) Response doGetReservedCharactersDisplay(); @GET @Path ( "/canCreate" ) @Produces ( MediaType.TEXT_PLAIN ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully returns true or false depending on the users permissions" ) } ) String doGetCanCreate(); @GET @Path ( "{pathId : .+}/acl" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Returns the requested file permissions in xml or json format" ), @ResponseCode ( code = 500, condition = "File failed to be retrieved. This could be caused by an invalid path, or the file does not exist." ) } ) RepositoryFileAclDto doGetFileAcl( @PathParam ( "pathId" ) String pathId ); @GET @Path ( "{pathId : .+}/properties" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the properties for a file." ), @ResponseCode ( code = 204, condition = "Invalid file path." ) } ) RepositoryFileDto doGetProperties( @PathParam ( "pathId" ) String pathId ); @GET @Path ( "{pathId : .+}/creator" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @Facet ( name = "Unsupported" ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the content creator for a file." ), @ResponseCode ( code = 403, condition = "Failure to move the file due to path not found." ) } ) RepositoryFileDto doGetContentCreator( @PathParam ( "pathId" ) String pathId ); @GET @Path ( "{pathId : .+}/generatedContent" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the list of RepositoryFileDto objects." ), @ResponseCode ( code = 200, condition = "Empty list of RepositoryFileDto objects." ) } ) List<RepositoryFileDto> doGetGeneratedContent( @PathParam ( "pathId" ) String pathId ); @GET @Path ( "{pathId : .+}/generatedContentForUser" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the list of RepositoryFileDto objects." ), @ResponseCode ( code = 200, condition = "Empty list of RepositoryFileDto objects." ), @ResponseCode ( code = 500, condition = "Server Error." ) } ) List<RepositoryFileDto> doGetGeneratedContentForUser( @PathParam ( "pathId" ) String pathId,
                                                               @QueryParam ( "user" ) String user ); @GET @Path ( "/tree" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the list of files from root of the repository." ), @ResponseCode ( code = 404, condition = "Invalid parameters." ), @ResponseCode ( code = 500, condition = "Server Error." ) } ) RepositoryFileTreeDto doGetRootTree( @QueryParam ( "depth" ) Integer depth,
                                              @QueryParam ( "filter" ) String filter, @QueryParam ( "showHidden" ) Boolean showHidden,
                                              @DefaultValue ( "false" ) @QueryParam ( "includeAcls" ) Boolean includeAcls ); @GET @Path ( "/children" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the list of child files from root of the repository." ), @ResponseCode ( code = 500, condition = "Server Error." ) } ) List<RepositoryFileDto> doGetRootChildren( @QueryParam ( "filter" ) String filter,
                                                    @QueryParam ( "showHidden" ) Boolean showHidden,
                                                    @DefaultValue ( "false" ) @QueryParam ( "includeAcls" ) Boolean includeAcls ); @GET @Path ( "{pathId : .+}/tree" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the list of files from root of the repository." ), @ResponseCode ( code = 404, condition = "Invalid parameters." ), @ResponseCode ( code = 500, condition = "Server Error." ) } ) RepositoryFileTreeDto doGetTree( @PathParam ( "pathId" ) String pathId, @QueryParam ( "depth" ) Integer depth,
                                          @QueryParam ( "filter" ) String filter, @QueryParam ( "showHidden" ) Boolean showHidden,
                                          @DefaultValue ( "false" ) @QueryParam ( "includeAcls" ) Boolean includeAcls,
                                          @DefaultValue ( "false" ) @QueryParam ( "includeSysDirs" ) Boolean includeSystemFolders ); @GET @Path ( "{pathId : .+}/children" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode( code = 200, condition = "Successfully retrieved the list of child files from selected repository path of the repository." ), @ResponseCode( code = 500, condition = "Server Error." ) } ) List<RepositoryFileDto> doGetChildren( @PathParam ( "pathId" ) String pathId,
                                                @QueryParam ( "filter" ) String filter, @QueryParam ( "showHidden" ) Boolean showHidden,
                                                @DefaultValue ( "false" ) @QueryParam ( "includeAcls" ) Boolean includeAcls ); @GET @Path ( "/deleted" ) @Produces ( {MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved the list of files from trash folder of the repository." ), @ResponseCode ( code = 500, condition = "Server Error." ) } ) List<RepositoryFileDto> doGetDeletedFiles(); @GET @Path ( "{pathId : .+}/metadata" ) @Produces ( { MediaType.APPLICATION_JSON } ) @StatusCodes( { @ResponseCode( code = 200, condition = "Successfully retrieved metadata." ), @ResponseCode( code = 403, condition = "Invalid path." ), @ResponseCode( code = 500, condition = "Server Error." ) } ) List<StringKeyStringValueDto> doGetMetadata( @PathParam ( "pathId" ) String pathId ); @PUT @Path ( "{pathId : .+}/rename" ) @Consumes ( { MediaType.WILDCARD } ) @Produces ( { MediaType.WILDCARD } ) @StatusCodes( { @ResponseCode( code = 200, condition = "Successfully renamed file." ), @ResponseCode( code = 403, condition = "Forbidden." ), @ResponseCode( code = 404, condition = "File not found." ) } ) Response doRename( @PathParam( "pathId" ) String pathId, @QueryParam( "newName" ) String newName ); @PUT @Path ( "{pathId : .+}/metadata" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully retrieved metadata." ), @ResponseCode ( code = 403, condition = "Invalid path." ), @ResponseCode ( code = 400, condition = "Invalid payload." ), @ResponseCode ( code = 500, condition = "Server Error." ) } ) Response doSetMetadata( @PathParam ( "pathId" ) String pathId, List<StringKeyStringValueDto> metadata ); @PUT @Path ( "{pathId : .+}/createDir" ) @Consumes ( { MediaType.WILDCARD } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Successfully created folder." ), @ResponseCode ( code = 403, condition = "Forbidden." ), @ResponseCode ( code = 409, condition = "Path already exists." ), @ResponseCode ( code = 500, condition = "Server Error." ) } ) Response doCreateDirs( @PathParam ( "pathId" ) String pathId ); @GET @Path ( "{pathId : .+}/versioningConfiguration" ) @Produces ( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @StatusCodes ( { @ResponseCode( code = 200, condition = "Successfully returns the versioning configuration" ) } ) FileVersioningConfiguration doVersioningConfiguration( @PathParam( "pathId" ) String pathId ); @GET @Path ( "/canDownload" ) @Produces ( { MediaType.TEXT_PLAIN } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Returns a boolean response." ) } ) Response canDownload( @QueryParam ( "dirPath" ) @DefaultValue( "" ) String dirPath ); @GET @Path ( "/canUpload" ) @Produces ( { MediaType.TEXT_PLAIN } ) @StatusCodes ( { @ResponseCode ( code = 200, condition = "Returns a boolean response." ) } ) Response canUpload( @QueryParam ( "dirPath" ) @DefaultValue( "" ) String dirPath ); RepositoryDownloadWhitelist getWhitelist(); void setWhitelist( RepositoryDownloadWhitelist whitelist ); static IAuthorizationPolicy getPolicy(); static IUnifiedRepository getRepository(); static DefaultUnifiedRepositoryWebService getRepoWs(); void setConverterHandler( IRepositoryContentConverterHandler converterHandler ); void setMimeResolver( IPlatformMimeResolver mimeResolver ); static final String APPLICATION_ZIP; static final String REPOSITORY_ADMIN_USERNAME; }
@Test public void testCreateFile() throws Exception { String charsetName = "charsetName"; InputStream mockInputStream = mock( InputStream.class ); doNothing().when( fileResource.fileService ).createFile( charsetName, PATH_ID, mockInputStream ); Response mockResponse = mock( Response.class ); doReturn( mockResponse ).when( fileResource ).buildOkResponse(); doReturn( charsetName ).when( fileResource.httpServletRequest ).getCharacterEncoding(); Response testResponse = fileResource.createFile( PATH_ID, mockInputStream ); assertEquals( mockResponse, testResponse ); verify( fileResource.fileService, times( 1 ) ).createFile( charsetName, PATH_ID, mockInputStream ); verify( fileResource, times( 1 ) ).buildOkResponse(); }