Example usage for com.liferay.portal.kernel.portlet PortletResponseUtil sendFile

List of usage examples for com.liferay.portal.kernel.portlet PortletResponseUtil sendFile

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.portlet PortletResponseUtil sendFile.

Prototype

public static void sendFile(PortletRequest portletRequest, MimeResponse mimeResponse, String fileName,
            InputStream inputStream, String contentType) throws IOException 

Source Link

Usage

From source file:com.liferay.testmisc.portlet.TestPortlet.java

License:Open Source License

@Override
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws IOException {

    String fileName = resourceRequest.getResourceID();

    InputStream inputStream = getPortletContext().getResourceAsStream("/WEB-INF/images/logo.png");

    String contentType = MimeTypesUtil.getContentType(fileName);

    PortletResponseUtil.sendFile(null, resourceResponse, fileName, inputStream, contentType);
}

From source file:com.liferay.users.admin.web.internal.portlet.action.ExportUsersMVCResourceCommand.java

License:Open Source License

@Override
protected void doServeResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws Exception {

    try {// w ww .  jav a 2 s .  c o  m
        SessionMessages.add(resourceRequest,
                _portal.getPortletId(resourceRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

        String csv = getUsersCSV(resourceRequest, resourceResponse);

        PortletResponseUtil.sendFile(resourceRequest, resourceResponse, "users.csv", csv.getBytes(),
                ContentTypes.TEXT_CSV_UTF8);
    } catch (Exception e) {
        SessionErrors.add(resourceRequest, e.getClass());

        _log.error(e, e);
    }
}

From source file:com.liferay.webform.portlet.WebFormPortlet.java

License:Open Source License

protected void exportData(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String portletId = PortalUtil.getPortletId(resourceRequest);

    PortletPermissionUtil.check(themeDisplay.getPermissionChecker(), themeDisplay.getPlid(), portletId,
            ActionKeys.CONFIGURATION);/*from   w ww . j  ava  2 s  . c  o m*/

    PortletPreferences preferences = PortletPreferencesFactoryUtil.getPortletSetup(resourceRequest);

    String databaseTableName = preferences.getValue("databaseTableName", StringPool.BLANK);
    String title = preferences.getValue("title", "no-title");

    StringBundler sb = new StringBundler();

    List<String> fieldLabels = new ArrayList<String>();

    for (int i = 1; true; i++) {
        String fieldLabel = preferences.getValue("fieldLabel" + i, StringPool.BLANK);

        String localizedfieldLabel = LocalizationUtil.getPreferencesValue(preferences, "fieldLabel" + i,
                themeDisplay.getLanguageId());

        if (Validator.isNull(fieldLabel)) {
            break;
        }

        fieldLabels.add(fieldLabel);

        sb.append(getCSVFormattedValue(localizedfieldLabel));
        sb.append(PortletPropsValues.CSV_SEPARATOR);
    }

    sb.setIndex(sb.index() - 1);

    sb.append(CharPool.NEW_LINE);

    if (Validator.isNotNull(databaseTableName)) {
        List<ExpandoRow> rows = ExpandoRowLocalServiceUtil.getRows(themeDisplay.getCompanyId(),
                WebFormUtil.class.getName(), databaseTableName, QueryUtil.ALL_POS, QueryUtil.ALL_POS);

        for (ExpandoRow row : rows) {
            for (String fieldName : fieldLabels) {
                String data = ExpandoValueLocalServiceUtil.getData(themeDisplay.getCompanyId(),
                        WebFormUtil.class.getName(), databaseTableName, fieldName, row.getClassPK(),
                        StringPool.BLANK);

                sb.append(getCSVFormattedValue(data));
                sb.append(PortletPropsValues.CSV_SEPARATOR);
            }

            sb.setIndex(sb.index() - 1);

            sb.append(CharPool.NEW_LINE);
        }
    }

    String fileName = title + ".csv";
    byte[] bytes = sb.toString().getBytes();
    String contentType = ContentTypes.APPLICATION_TEXT;

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, fileName, bytes, contentType);
}

From source file:com.siemens.sw360.portal.common.AttachmentPortletUtils.java

License:Open Source License

public void serveFile(ResourceRequest request, ResourceResponse response) {
    String id = request.getParameter(PortalConstants.ATTACHMENT_ID);

    try {//  w w w  .ja v a2  s  .  c  om
        AttachmentContent attachment = client.getAttachmentContent(id);
        InputStream attachmentStream = getConnector().getAttachmentStream(attachment);
        try {
            PortletResponseUtil.sendFile(request, response, attachment.getFilename(), attachmentStream,
                    attachment.getContentType());
        } catch (IOException e) {
            log.error("cannot finish writing response", e);
            response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
        } finally {
            closeQuietly(attachmentStream, log);
        }
    } catch (TException e) {
        log.error("Problem getting the attachment content from the backend", e);
    }
}

From source file:com.siemens.sw360.portal.portlets.admin.ComponentUploadPortlet.java

License:Open Source License

private void generateSampleReleaseLinksFile(ResourceRequest request, ResourceResponse response)
        throws IOException {
    final Iterable<String> csvHeaderIterable = ReleaseLinkCSVRecord.getCSVHeaderIterable();
    final Iterable<Iterable<String>> inputIterable = ImmutableList
            .of(ReleaseLinkCSVRecord.getSampleInputIterable());
    ByteArrayInputStream byteArrayInputStream = CSVExport.createCSV(csvHeaderIterable, inputIterable);
    PortletResponseUtil.sendFile(request, response, "ReleaseLinkInfo_Sample.csv", byteArrayInputStream,
            "text/csv");
}

From source file:com.siemens.sw360.portal.portlets.admin.ComponentUploadPortlet.java

License:Open Source License

private void generateReleaseLinksFile(ResourceRequest request, ResourceResponse response) throws IOException {

    List<Iterable<String>> csvRows = new ArrayList<>();
    final List<Component> componentDetailedSummaryForExport = getComponentDetailedSummaryForExport();
    if (componentDetailedSummaryForExport != null) {
        final Map<String, Component> componentsById = ThriftUtils.getIdMap(componentDetailedSummaryForExport);
        final Map<String, Release> releasesById = getReleasesById(componentDetailedSummaryForExport);

        for (Component component : componentDetailedSummaryForExport) {
            dealWithReleaseLinksContainedInComponent(componentsById, releasesById, component, csvRows);
        }//from w  w w  . j av a  2 s.co  m
    }

    ByteArrayInputStream byteArrayInputStream = CSVExport.createCSV(ReleaseLinkCSVRecord.getCSVHeaderIterable(),
            csvRows);
    PortletResponseUtil.sendFile(request, response, "ReleaseLinkInfo.csv", byteArrayInputStream, "text/csv");
}

From source file:com.siemens.sw360.portal.portlets.admin.ComponentUploadPortlet.java

License:Open Source License

private void generateAttachmentsFile(ResourceRequest request, ResourceResponse response) throws IOException {
    List<Iterable<String>> csvRows = new ArrayList<>();
    final List<Component> componentDetailedSummaryForExport = getComponentDetailedSummaryForExport();
    if (componentDetailedSummaryForExport != null) {
        for (Component component : componentDetailedSummaryForExport) {
            printComponentAttachments(component, csvRows);
            printReleasesAttachments(component, csvRows);
        }/*from w w w.  j av  a2 s .com*/
    }

    ByteArrayInputStream byteArrayInputStream = CSVExport
            .createCSV(ComponentAttachmentCSVRecord.getCSVHeaderIterable(), csvRows);
    PortletResponseUtil.sendFile(request, response, "AttachmentInfo.csv", byteArrayInputStream, "text/csv");
}

From source file:com.siemens.sw360.portal.portlets.admin.ComponentUploadPortlet.java

License:Open Source License

private void generateSampleAttachmentsFile(ResourceRequest request, ResourceResponse response)
        throws IOException {
    final Iterable<String> csvHeaderIterable = ComponentAttachmentCSVRecord.getCSVHeaderIterable();
    final Iterable<Iterable<String>> inputIterable = ImmutableList
            .of(ComponentAttachmentCSVRecord.getSampleInputIterable());

    ByteArrayInputStream byteArrayInputStream = CSVExport.createCSV(csvHeaderIterable, inputIterable);
    PortletResponseUtil.sendFile(request, response, "AttachmentInfo_Sample.csv", byteArrayInputStream,
            "text/csv");
}

From source file:com.siemens.sw360.portal.portlets.admin.ComponentUploadPortlet.java

License:Open Source License

public void generateSampleFile(ResourceRequest request, ResourceResponse response) throws IOException {
    final Iterable<Iterable<String>> inputIterable = ImmutableList
            .of(ComponentCSVRecord.getSampleInputIterable());
    final Iterable<String> csvHeaderIterable = ComponentCSVRecord.getCSVHeaderIterable();

    ByteArrayInputStream byteArrayInputStream = CSVExport.createCSV(csvHeaderIterable, inputIterable);
    PortletResponseUtil.sendFile(request, response, "ComponentsReleasesVendorsSample.csv", byteArrayInputStream,
            "text/csv");
}

From source file:com.siemens.sw360.portal.portlets.admin.ComponentUploadPortlet.java

License:Open Source License

public void backUpComponents(ResourceRequest request, ResourceResponse response) throws IOException {
    final Iterable<String> csvHeaderIterable = ComponentCSVRecord.getCSVHeaderIterable();
    final List<Component> componentDetailedSummaryForExport = getComponentDetailedSummaryForExport();
    List<Iterable<String>> csvRows = getFlattenedView(componentDetailedSummaryForExport);

    ByteArrayInputStream byteArrayInputStream = CSVExport.createCSV(csvHeaderIterable, csvRows);
    PortletResponseUtil.sendFile(request, response, "ComponentsReleasesVendors.csv", byteArrayInputStream,
            "text/csv");
}