Example usage for org.springframework.core.io ByteArrayResource getByteArray

List of usage examples for org.springframework.core.io ByteArrayResource getByteArray

Introduction

In this page you can find the example usage for org.springframework.core.io ByteArrayResource getByteArray.

Prototype

public final byte[] getByteArray() 

Source Link

Document

Return the underlying byte array.

Usage

From source file:com.flipkart.phantom.runtime.impl.spring.admin.SPConfigServiceImpl.java

/**
 * Interface method implementation./* ww w  .  j a  v a 2s.c o m*/
 * @see com.flipkart.phantom.runtime.spi.spring.admin.SPConfigService#modifyHandlerConfig(java.lang.String, org.springframework.core.io.ByteArrayResource)
 */
public void modifyHandlerConfig(String handlerName, ByteArrayResource modifiedHandlerConfigFile) {

    // Check if exists
    File oldHandlerFile = null;
    try {
        oldHandlerFile = this.getHandlerConfig(handlerName).getFile();
    } catch (IOException e1) {
        LOGGER.error("Handler Config File for handler: " + handlerName + " not found. Returning");
        throw new PlatformException("File not found for handler: " + handlerName, e1);
    }
    if (!oldHandlerFile.exists()) {
        LOGGER.error("Handler Config File: " + oldHandlerFile.getAbsolutePath() + " doesn't exist. Returning");
        throw new PlatformException("File not found: " + oldHandlerFile.getAbsolutePath());
    }

    // Check for read permissions
    if (!oldHandlerFile.canRead()) {
        LOGGER.error("No read permission for: " + oldHandlerFile.getAbsolutePath() + ". Returning");
        throw new PlatformException("Read permissions not found for file: " + oldHandlerFile.getAbsolutePath());
    }

    // Check for write permissions
    if (!oldHandlerFile.canWrite()) {
        LOGGER.error("No write permission for: " + oldHandlerFile.getAbsolutePath()
                + ". Write permissions are required to modify handler confib");
        throw new PlatformException(
                "Required permissions not found for modifying file: " + oldHandlerFile.getAbsolutePath());
    }

    // create backup
    this.createPrevConfigFile(handlerName);

    // file_put_contents Java :-/
    try {
        this.upload(modifiedHandlerConfigFile.getByteArray(), oldHandlerFile.getAbsolutePath());
    } catch (IOException e) {
        LOGGER.error("IOException while uploading file to path: " + oldHandlerFile.getAbsolutePath());
        this.restorePrevConfigFile(handlerName);
        throw new PlatformException(e);
    }

    // get the registered AbstractHandler for the file name
    AbstractHandler handler = this.configURItoHandlerName.get(oldHandlerFile.toURI()).get(0);
    // re-load the handler
    // TODO
    // loading component destroys all beans in the config file given by the handler config info
    // this mean this only works if only one handler per handler xml file
    try {
        this.componentContainer.reloadHandler(handler, this.getHandlerConfig(handlerName));
    } catch (Exception e) {
        this.restorePrevConfigFile(handlerName);
        this.componentContainer.loadComponent(this.getHandlerConfig(handlerName));
        throw new PlatformException(e);
    }
    this.removePrevConfigFile(handlerName);
}

From source file:com.flipkart.aesop.runtime.impl.admin.RuntimeConfigServiceImpl.java

/**
 * Interface method implementation./*from   ww  w  .j  a  v  a  2s . c om*/
 * @see com.flipkart.aesop.runtime.spi.admin.RuntimeConfigService#modifyRuntimeConfig(java.lang.String, org.springframework.core.io.ByteArrayResource)
 */
public void modifyRuntimeConfig(String runtimeName, ByteArrayResource modifiedRuntimeConfigFile)
        throws PlatformException {
    // Check if exists
    File oldRuntimeFile = null;
    try {
        oldRuntimeFile = this.getRuntimeConfig(runtimeName).getFile();
    } catch (IOException e1) {
        LOGGER.error("Config File for runtime: " + runtimeName + " not found. Returning");
        throw new PlatformException("File not found for runtime: " + runtimeName, e1);
    }
    if (!oldRuntimeFile.exists()) {
        LOGGER.error("Runtime Config File: " + oldRuntimeFile.getAbsolutePath() + " doesn't exist. Returning");
        throw new PlatformException("File not found: " + oldRuntimeFile.getAbsolutePath());
    }

    // Check for read permissions
    if (!oldRuntimeFile.canRead()) {
        LOGGER.error("No read permission for: " + oldRuntimeFile.getAbsolutePath() + ". Returning");
        throw new PlatformException("Read permissions not found for file: " + oldRuntimeFile.getAbsolutePath());
    }

    // Check for write permissions
    if (!oldRuntimeFile.canWrite()) {
        LOGGER.error("No write permission for: " + oldRuntimeFile.getAbsolutePath()
                + ". Write permissions are required to modify runtime config");
        throw new PlatformException(
                "Required permissions not found for modifying file: " + oldRuntimeFile.getAbsolutePath());
    }

    // create backup
    this.createPrevConfigFile(runtimeName);

    // file_put_contents Java :-/
    try {
        this.upload(modifiedRuntimeConfigFile.getByteArray(), oldRuntimeFile.getAbsolutePath());
    } catch (IOException e) {
        LOGGER.error("IOException while uploading file to path: " + oldRuntimeFile.getAbsolutePath());
        this.restorePrevConfigFile(runtimeName);
        throw new PlatformException(e);
    }

    // get the registered ServerContainer for the file name
    ServerContainer runtime = this.configURItoRuntimeName.get(oldRuntimeFile.toURI()).get(0);
    // re-load the runtime
    // TODO
    // loading component destroys all beans in the config file given by the runtime config info
    // this mean this only works if only one runtime per runtime xml file
    try {
        this.componentContainer.reloadRuntime(runtime, this.getRuntimeConfig(runtimeName));
    } catch (Exception e) {
        this.restorePrevConfigFile(runtimeName);
        this.componentContainer.loadComponent(this.getRuntimeConfig(runtimeName));
        throw new PlatformException(e);
    }
    this.removePrevConfigFile(runtimeName);
}

From source file:com.photon.phresco.service.rest.api.ComponentService.java

private void saveOrUpdatePilot(ByteArrayResource pilotFile, byte[] pilotData) throws PhrescoException {
    ApplicationInfo applicationInfo = new Gson().fromJson(new String(pilotData), ApplicationInfo.class);
    if (pilotFile != null) {
        boolean saveArtifactFile = saveArtifactFile(applicationInfo.getPilotContent(),
                pilotFile.getByteArray());
        if (!saveArtifactFile) {
            throw new PhrescoException("Unable to create pilot...");
        }//w w  w. jav  a 2  s. c o m
    }
    saveApplicationInfo(applicationInfo);
}

From source file:com.photon.phresco.service.rest.api.ComponentService.java

private void saveOrUpdateDownloads(ByteArrayResource downloadFile, ByteArrayResource iconFile,
        byte[] downloadData) throws PhrescoException {
    DownloadInfo downloadInfo = new Gson().fromJson(new String(downloadData), DownloadInfo.class);
    if (downloadFile != null) {
        boolean saveArtifactFile = saveArtifactFile(downloadInfo.getArtifactGroup(),
                downloadFile.getByteArray());
        if (!saveArtifactFile) {
            throw new PhrescoException("Unable to create download... ");
        }/* www. ja  v  a2s  .  c o  m*/
    }
    if (iconFile != null) {
        boolean saveArtifactIcon = saveArtifactIcon(downloadInfo.getArtifactGroup(), iconFile.getByteArray(),
                ICON_EXT);
        if (!saveArtifactIcon) {
            throw new PhrescoException("Unable to create download icon... ");
        }
    }
    saveDownloads(downloadInfo);
}

From source file:com.photon.phresco.service.rest.api.ComponentService.java

/**
 * Updates the list of modules/*from   www  . ja  v a  2  s .  co m*/
 * @param modules
 * @return 
 * @return
 * @throws PhrescoException 
 */
@ApiOperation(value = " Updates features ")
@ApiErrors(value = { @ApiError(code = 500, reason = "Feature updation failed") })
@RequestMapping(value = REST_API_MODULES, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE,
        MediaType.APPLICATION_JSON_VALUE,
        "multipart/mixed" }, produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.PUT)
public @ResponseBody ArtifactGroup updateModules(HttpServletResponse response,
        MultipartHttpServletRequest request,
        @RequestPart(value = "feature", required = false) ByteArrayResource moduleFile,
        @RequestPart(value = "icon", required = false) ByteArrayResource iconFile,
        @RequestParam("data") byte[] artifactGroupData) throws PhrescoException {
    if (isDebugEnabled) {
        LOGGER.debug("ComponentService.updateModules :  Entry");
        LOGGER.debug("ComponentService.findModules", "remoteAddress=" + request.getRemoteAddr(),
                "endpoint=" + request.getRequestURI(), "user=" + request.getParameter("userId"));
    }
    String string = new String(artifactGroupData);
    ArtifactGroup artifactGroup = new Gson().fromJson(string, ArtifactGroup.class);
    if (moduleFile != null) {
        boolean saveArtifactFile = saveArtifactFile(artifactGroup, moduleFile.getByteArray());
        if (!saveArtifactFile) {
            throw new PhrescoException("Unable to create artifact");
        }
    }
    if (iconFile != null) {
        artifactGroup.setPackaging(ICON_EXT);
        boolean saveArtifactFile = saveArtifactFile(artifactGroup, iconFile.getByteArray());
        if (!saveArtifactFile) {
            throw new PhrescoException("Unable to create artifact");
        }
    }
    saveModuleGroup(artifactGroup);
    return artifactGroup;
}

From source file:com.photon.phresco.service.rest.api.ComponentService.java

/**
 * Creates the list of modules/*from   w  ww .  ja v a 2s  .  c  o  m*/
 * @param modules
 * @return 
 * @return 
 * @throws PhrescoException 
 * @throws IOException 
 */
@ApiOperation(value = " Creates new features ")
@ApiErrors(value = { @ApiError(code = 500, reason = "Feature creation failed") })
@RequestMapping(value = REST_API_MODULES, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE,
        MediaType.APPLICATION_JSON_VALUE,
        "multipart/mixed" }, produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
public @ResponseBody ArtifactGroup createModules(HttpServletResponse response,
        MultipartHttpServletRequest request,
        @RequestPart(value = "feature", required = false) ByteArrayResource moduleFile,
        @RequestPart(value = "icon", required = false) ByteArrayResource iconFile,
        @RequestParam("moduleGroup") byte[] artifactGroupData) throws PhrescoException, IOException {
    if (isDebugEnabled) {
        LOGGER.debug("ComponentService.createModules : Entry");
        LOGGER.debug("ComponentService.findModules", "remoteAddress=" + request.getRemoteAddr(),
                "endpoint=" + request.getRequestURI(), "user=" + request.getParameter("userId"));
    }
    String string = new String(artifactGroupData);
    ArtifactGroup artifactGroup = new Gson().fromJson(string, ArtifactGroup.class);
    if (moduleFile != null) {
        boolean saveArtifactFile = saveArtifactFile(artifactGroup, moduleFile.getByteArray());
        if (!saveArtifactFile) {
            throw new PhrescoException("Unable to create artifact");
        }
    }
    if (iconFile != null) {
        //artifactGroup.setPackaging(ICON_EXT);
        boolean saveArtifactIcon = saveArtifactIcon(artifactGroup, iconFile.getByteArray(), ICON_EXT);
        if (!saveArtifactIcon) {
            throw new PhrescoException("Unable to create artifact");
        }
    }
    saveModuleGroup(artifactGroup);
    return artifactGroup;
}