Search in sources :

Example 86 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project coprhd-controller by CoprHD.

the class OrderService method downloadOrders.

/**
 * Get log data from the specified virtual machines that are filtered, merged,
 * and sorted based on the passed request parameters and streams the log
 * messages back to the client as JSON formatted strings.
 *
 * @brief Show logs from all or specified virtual machine
 * @param startTimeStr The start datetime of the desired time window. Value is
 *            inclusive.
 *            Allowed values: "yyyy-MM-dd_HH:mm:ss" formatted date or
 *            datetime in ms.
 *            Default: Set to yesterday same time
 * @param endTimeStr The end datetime of the desired time window. Value is
 *            inclusive.
 *            Allowed values: "yyyy-MM-dd_HH:mm:ss" formatted date or
 *            datetime in ms.
 * @param tenantIDsStr a list of tenant IDs separated by ','
 * @param orderIDsStr a list of order IDs separated by ','
 * @prereq one of tenantIDsStr and orderIDsStr should be empty
 * @return A reference to the StreamingOutput to which the log data is
 *         written.
 * @throws WebApplicationException When an invalid request is made.
 */
@GET
@CheckPermission(roles = { Role.SYSTEM_ADMIN, Role.SYSTEM_MONITOR, Role.SECURITY_ADMIN })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN })
@Path("/download")
public Response downloadOrders(@DefaultValue("") @QueryParam(SearchConstants.START_TIME_PARAM) String startTimeStr, @DefaultValue("") @QueryParam(SearchConstants.END_TIME_PARAM) String endTimeStr, @DefaultValue("") @QueryParam(SearchConstants.TENANT_IDS_PARAM) String tenantIDsStr, @DefaultValue("") @QueryParam(SearchConstants.ORDER_STATUS_PARAM2) String orderStatusStr, @DefaultValue("") @QueryParam(SearchConstants.ORDER_IDS) String orderIDsStr) throws Exception {
    if (tenantIDsStr.isEmpty() && orderIDsStr.isEmpty()) {
        InvalidParameterException cause = new InvalidParameterException("Both tenant and order IDs are empty");
        throw APIException.badRequests.invalidParameterWithCause(SearchConstants.TENANT_ID_PARAM, tenantIDsStr, cause);
    }
    final long startTimeInMS = getTime(startTimeStr, 0);
    final long endTimeInMS = getTime(endTimeStr, System.currentTimeMillis());
    if (startTimeInMS > endTimeInMS) {
        throw APIException.badRequests.endTimeBeforeStartTime(startTimeStr, endTimeStr);
    }
    OrderStatus orderStatus = getOrderStatus(orderStatusStr, false);
    if (isJobRunning()) {
        throw APIException.badRequests.cannotExecuteOperationWhilePendingTask("Deleting/Downloading orders");
    }
    final List<URI> tids = toIDs(SearchConstants.TENANT_IDS_PARAM, tenantIDsStr);
    StorageOSUser user = getUserFromContext();
    URI tid = URI.create(user.getTenantId());
    URI uid = URI.create(user.getName());
    final OrderJobStatus status = new OrderJobStatus(OrderServiceJob.JobType.DOWNLOAD_ORDER, startTimeInMS, endTimeInMS, tids, tid, uid, orderStatus);
    List<URI> orderIDs = toIDs(SearchConstants.ORDER_IDS, orderIDsStr);
    status.setTotal(orderIDs.size());
    if (!orderIDs.isEmpty()) {
        status.setStartTime(0);
        status.setEndTime(0);
    }
    try {
        saveJobInfo(status);
    } catch (Exception e) {
        log.error("Failed to save job info e=", e);
        throw APIException.internalServerErrors.getLockFailed();
    }
    StreamingOutput out = new StreamingOutput() {

        @Override
        public void write(OutputStream outputStream) {
            exportOrders(tids, orderIDsStr, startTimeInMS, endTimeInMS, outputStream, status);
        }
    };
    return Response.ok(out).build();
}
Also used : InvalidParameterException(java.security.InvalidParameterException) OrderStatus(com.emc.storageos.db.client.model.uimodels.OrderStatus) StorageOSUser(com.emc.storageos.security.authentication.StorageOSUser) OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) OrderJobStatus(com.emc.sa.api.utils.OrderJobStatus) URI(java.net.URI) InvalidParameterException(java.security.InvalidParameterException) WebApplicationException(javax.ws.rs.WebApplicationException) URISyntaxException(java.net.URISyntaxException) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Example 87 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project pentaho-platform by pentaho.

the class FileResource method doGetDirAsZip.

/**
 * @param repositoryFile
 * @return
 */
public Response doGetDirAsZip(RepositoryFile repositoryFile) {
    String path = repositoryFile.getPath();
    final InputStream is;
    try {
        Exporter exporter = getExporter();
        exporter.setRepoPath(path);
        exporter.setRepoWs(repoWs);
        File zipFile = exporter.doExportAsZip(repositoryFile);
        is = getFileInputStream(zipFile);
    } catch (Exception e) {
        return buildServerErrorResponse(e.toString());
    }
    StreamingOutput streamingOutput = getStreamingOutput(is);
    return buildOkResponse(streamingOutput, APPLICATION_ZIP);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) Exporter(org.pentaho.platform.plugin.services.importexport.Exporter) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) GeneralSecurityException(java.security.GeneralSecurityException) InvalidParameterException(java.security.InvalidParameterException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) UnifiedRepositoryAccessDeniedException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException) FileNotFoundException(java.io.FileNotFoundException) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) WebApplicationException(javax.ws.rs.WebApplicationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExportException(org.pentaho.platform.plugin.services.importexport.ExportException) DocumentException(org.dom4j.DocumentException) IllegalSelectorException(java.nio.channels.IllegalSelectorException) IOException(java.io.IOException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 88 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project pentaho-platform by pentaho.

the class FileServiceIT method testDoGetFileAsInline.

@Test
public void testDoGetFileAsInline() throws FileNotFoundException {
    /*
     * TEST 1
     */
    doReturn(true).when(fileService).isPath(nullable(String.class));
    doReturn(true).when(fileService).isPathValid(nullable(String.class));
    RepositoryDownloadWhitelist mockWhiteList = mock(RepositoryDownloadWhitelist.class);
    doReturn(mockWhiteList).when(fileService).getWhitelist();
    doReturn(true).when(mockWhiteList).accept(nullable(String.class));
    RepositoryFile mockRepoFile = mock(RepositoryFile.class);
    doReturn(mockRepoFile).when(fileService.repository).getFile(nullable(String.class));
    SimpleRepositoryFileData mockData = mock(SimpleRepositoryFileData.class);
    doReturn(mockData).when(fileService.repository).getDataForRead(any(Serializable.class), any(Class.class));
    InputStream mockInputStream = mock(InputStream.class);
    doReturn(mockInputStream).when(mockData).getInputStream();
    StreamingOutput mockStreamingOutput = mock(StreamingOutput.class);
    doReturn(mockStreamingOutput).when(fileService).getStreamingOutput(mockInputStream);
    FileService.RepositoryFileToStreamWrapper wrapper = fileService.doGetFileAsInline("test");
    verify(fileService.repository, times(1)).getFile(nullable(String.class));
    verify(mockWhiteList, times(1)).accept(nullable(String.class));
    verify(fileService, times(2)).getRepository();
    verify(fileService.repository, times(1)).getDataForRead(any(Serializable.class), any(Class.class));
    verify(mockData, times(1)).getInputStream();
    assertEquals(mockRepoFile, wrapper.getRepositoryFile());
    assertEquals(mockStreamingOutput, wrapper.getOutputStream());
    /*
     * TEST 2
     */
    doReturn(false).when(fileService).isPath(nullable(String.class));
    doReturn(mockRepoFile).when(fileService.repository).getFileById(nullable(String.class));
    wrapper = fileService.doGetFileAsInline("test");
    verify(fileService.repository, times(1)).getFileById(nullable(String.class));
    verify(fileService, times(4)).getRepository();
    assertEquals(mockRepoFile, wrapper.getRepositoryFile());
    assertEquals(mockStreamingOutput, wrapper.getOutputStream());
}
Also used : Serializable(java.io.Serializable) RepositoryDownloadWhitelist(org.pentaho.platform.repository.RepositoryDownloadWhitelist) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) StreamingOutput(javax.ws.rs.core.StreamingOutput) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 89 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project pentaho-platform by pentaho.

the class FileService method systemBackup.

public DownloadFileWrapper systemBackup(String userAgent) throws IOException, ExportException {
    if (doCanAdminister()) {
        String originalFileName;
        String encodedFileName;
        originalFileName = "SystemBackup.zip";
        encodedFileName = makeEncodedFileName(originalFileName);
        StreamingOutput streamingOutput = getBackupStream();
        final String attachment = HttpMimeTypeListener.buildContentDispositionValue(originalFileName, true);
        return new DownloadFileWrapper(streamingOutput, attachment, encodedFileName);
    } else {
        throw new SecurityException();
    }
}
Also used : StreamingOutput(javax.ws.rs.core.StreamingOutput) GeneralSecurityException(java.security.GeneralSecurityException)

Example 90 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project pentaho-platform by pentaho.

the class FileService method getBackupStream.

private StreamingOutput getBackupStream() throws IOException, ExportException {
    File zipFile = getBackupExporter().performExport();
    final FileInputStream inputStream = new FileInputStream(zipFile);
    return new StreamingOutput() {

        @Override
        public void write(OutputStream output) throws IOException {
            IOUtils.copy(inputStream, output);
        }
    };
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) RepositoryFileOutputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputStream) OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) FileInputStream(java.io.FileInputStream)

Aggregations

StreamingOutput (javax.ws.rs.core.StreamingOutput)190 OutputStream (java.io.OutputStream)84 Response (javax.ws.rs.core.Response)76 Path (javax.ws.rs.Path)53 Produces (javax.ws.rs.Produces)52 IOException (java.io.IOException)50 GET (javax.ws.rs.GET)50 File (java.io.File)45 InputStream (java.io.InputStream)45 Test (org.junit.Test)44 WebApplicationException (javax.ws.rs.WebApplicationException)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)32 List (java.util.List)26 MediaType (javax.ws.rs.core.MediaType)24 ByteArrayInputStream (java.io.ByteArrayInputStream)20 ArrayList (java.util.ArrayList)20 Consumes (javax.ws.rs.Consumes)20 HashMap (java.util.HashMap)19 POST (javax.ws.rs.POST)19 FileOutputStream (java.io.FileOutputStream)17