Example usage for com.liferay.portal.kernel.util FileUtil getFile

List of usage examples for com.liferay.portal.kernel.util FileUtil getFile

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util FileUtil getFile.

Prototype

public static com.liferay.portal.kernel.util.File getFile() 

Source Link

Usage

From source file:com.celamanzi.liferay.portlets.rails286.Rails286Portlet.java

License:Open Source License

/**
 * Used to download files and serve Ajax requests.
 *///w  w w . j  a va2 s.c o m
@Override
public void serveResource(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException {
    log.debug("serveResource has been called");
    super.serveResource(request, response);

    // Retrieve parameters
    Map<String, String[]> params = Rails286PortletFunctions.mapRequestParameters(request);
    NameValuePair[] parametersBody = Rails286PortletFunctions.paramsToNameValuePairs(params);

    String actionMethod = (params.containsKey("originalActionMethod") ? params.remove("originalActionMethod")[0]
            : "post");

    // Adding the parameters to request for callRails
    request.getPortletSession(true).setAttribute("requestMethod", actionMethod);
    request.setAttribute("parametersBody", parametersBody);

    /* is this Ajax?
    request headers are not accessible..
    */
    //__headers['X_REQUESTED_WITH'] = 'XMLHttpRequest'
    //__headers['ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*'
    //__content_type = 'application/x-www-form-urlencoded; charset=UTF-8'
    /* .. so this is the best I could find..
    */
    if (request.getResponseContentType().equals("text/html")) {
        // .. this is Ajax.
        log.debug("Ajax (text/html)");
        request.setAttribute("X_REQUESTED_WITH", "XMLHttpRequest");
        byte[] railsBytes = callRails(request, response);
        try {
            PortletResponseUtil.write(response, railsBytes);
        } catch (IOException e) {
            log.error("Exception: " + e.getMessage());
        }
        return;
    }

    // else .. download
    byte[] railsBytes = callRails(request, response);

    /**
    When we write the bytes directly to the output of the portlet,
    we obtained corrupted files (pdf, png, zip, tar).
    - tulios
    */
    String filename = getFilename();
    if (filename != null && !filename.equals("")) {
        File file = null;
        try {
            file = new File(Rails286PortletFunctions.getTempPath() + "/" + filename);
            log.debug("downloading contents to " + file.toString());
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(railsBytes);
            fos.flush();
            fos.close();

            // This "if" is to avoid this call when in a test environment, because liferay test environment is too
            // heavy and dirty to be implemented =[ (sorry...)
            if (FileUtil.getFile() != null) {
                PortletResponseUtil.sendFile(request, response, filename, new FileInputStream(file));
                // only delete the temporary file in live environment, not during test
                if (!file.delete()) {
                    log.error("Failed to delete temporary file " + file.getAbsolutePath());
                }
            }
        } catch (IOException e) {
            log.error("Exception: " + e.getMessage());
            if (file != null) {
                log.error("File path: " + file.getAbsolutePath());
            }
        }
    }
}

From source file:com.liferay.alloy.tools.xmlbuilder.XMLBuilder.java

License:Open Source License

public XMLBuilder(String componentsJSON, String componentsXML, String componentExcluded) throws Exception {

    if (FileUtil.getFile() == null) {
        (new FileUtil()).setFile(new FileImpl());
    }/*from  w w w .ja v  a  2s  .co  m*/

    if (SAXReaderUtil.getSAXReader() == null) {
        (new SAXReaderUtil()).setSAXReader(new SAXReaderImpl());
    }

    _componentJSON = componentsJSON;
    _componentXML = componentsXML;
    _componentExcluded = Arrays.asList(StringUtil.split(componentExcluded));

    _fileXML = new File(_componentXML);
    _fileJSON = new File(_componentJSON);

    _json = new JSONObject(FileUtil.read(_fileJSON));
    _classMapJSON = _json.getJSONObject("classmap");

    _create();
}