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.siemens.sw360.portal.portlets.admin.ComponentUploadPortlet.java

License:Open Source License

public void backUpLicenses(ResourceRequest request, ResourceResponse response) throws IOException, TException {
    Map<String, InputStream> fileNameToStreams = getFilenameToCSVStreams();

    final ByteArrayOutputStream outB = new ByteArrayOutputStream();
    final ZipOutputStream zipOutputStream = new ZipOutputStream(outB);

    for (Map.Entry<String, InputStream> entry : fileNameToStreams.entrySet()) {
        ZipTools.addToZip(zipOutputStream, entry.getKey(), entry.getValue());
    }/*from  w ww . jav a 2 s . c  o m*/

    zipOutputStream.flush();
    zipOutputStream.close(); // this closes outB

    final ByteArrayInputStream zipFile = new ByteArrayInputStream(outB.toByteArray());
    PortletResponseUtil.sendFile(request, response, "LicensesBackup.lics", zipFile, "application/zip");
}

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

License:Open Source License

private void servePublicKeyFile(ResourceRequest request, ResourceResponse response) {
    try {//from   w  w w  .  j  ava  2 s .  c  o  m
        String publicKey = thriftClients.makeFossologyClient().getPublicKey();

        final ByteArrayInputStream keyStream = new ByteArrayInputStream(publicKey.getBytes());
        PortletResponseUtil.sendFile(request, response, "sw360_id.pub", keyStream, "text/plain");
    } catch (IOException | TException e) {
        log.error("An error occurred while retrieving the public key", e);
    }
}

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

License:Open Source License

public void backUpUsers(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException, SystemException, PortalException {
    List<User> liferayUsers;
    try {/*from   w ww . j  a v a  2 s  .c  om*/
        liferayUsers = UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
    } catch (SystemException e) {
        log.error("Could not get user List from liferay", e);
        liferayUsers = Collections.emptyList();
    }

    final ByteArrayOutputStream outB = new ByteArrayOutputStream();
    Writer out = new BufferedWriter(new OutputStreamWriter(outB));

    CSVPrinter csvPrinter = new CSVPrinter(out, CommonUtils.sw360CsvFormat);

    csvPrinter.printRecord("GivenName", "Lastname", "Email", "Department", "UserGroup", "GID", "isMale",
            "PasswdHash", "wantsMailNotification");
    for (User liferayUser : liferayUsers) {

        String firstName = liferayUser.getFirstName();
        String lastName = liferayUser.getLastName();
        String emailAddress = liferayUser.getEmailAddress();
        List<Organization> organizations = liferayUser.getOrganizations();

        String department = "";

        if (organizations != null && organizations.size() > 0) {
            department = organizations.get(0).getName();
        }

        String gid = liferayUser.getOpenId();
        boolean isMale = liferayUser.isMale();
        String passwordHash = liferayUser.getPassword();
        if (isNullOrEmpty(emailAddress) || isNullOrEmpty(department)) {
            continue;
        }
        com.siemens.sw360.datahandler.thrift.users.User sw360user = UserCacheHolder
                .getUserFromEmail(emailAddress);
        boolean wantsMailNotification = sw360user.isSetWantsMailNotification() ? sw360user.wantsMailNotification
                : true;
        String userGroup = sw360user.getUserGroup().toString();

        csvPrinter.printRecord(firstName, lastName, emailAddress, department, userGroup, gid, isMale,
                passwordHash, wantsMailNotification);
    }

    csvPrinter.flush();
    csvPrinter.close();

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outB.toByteArray());
    PortletResponseUtil.sendFile(request, response, "Users.csv", byteArrayInputStream, "text/csv");
}

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

License:Open Source License

private void exportExcel(ResourceRequest request, ResourceResponse response) {
    try {//from   w  w w .j a va2  s  .c  o m
        VendorService.Iface client = thriftClients.makeVendorClient();
        List<Vendor> vendors = client.getAllVendors();

        PortletResponseUtil.sendFile(request, response, "Vendors.xlsx", exporter.makeExcelExport(vendors),
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    } catch (IOException | TException e) {
        log.error("An error occured while generating the Excel export", e);
    }
}

From source file:com.siemens.sw360.portal.portlets.components.ComponentPortlet.java

License:Open Source License

private void exportExcel(ResourceRequest request, ResourceResponse response) {
    try {//from w  ww . java2s. c  om
        ComponentService.Iface client = thriftClients.makeComponentClient();
        String searchText = request.getParameter(PortalConstants.KEY_SEARCH_TEXT);
        List<Component> components;

        if (isNullOrEmpty(searchText)) {
            components = client.getComponentSummaryForExport();
        } else {
            components = client.searchComponentForExport(searchText);

        }

        ComponentExporter exporter = new ComponentExporter();
        PortletResponseUtil.sendFile(request, response, "Components.xlsx", exporter.makeExcelExport(components),
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    } catch (IOException | TException e) {
        log.error("An error occurred while generating the Excel export", e);
    }
}

From source file:com.siemens.sw360.portal.portlets.licenses.LicensesPortlet.java

License:Open Source License

private void exportExcel(ResourceRequest request, ResourceResponse response) {
    try {//from   w ww.j a  v a2  s. co  m
        LicenseService.Iface client = thriftClients.makeLicenseClient();
        List<License> licenses = client.getLicenseSummaryForExport();

        PortletResponseUtil.sendFile(request, response, "Licenses.xlsx", exporter.makeExcelExport(licenses),
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    } catch (IOException | TException e) {
        log.error("An error occurred while generating the Excel export", e);
    }
}

From source file:com.siemens.sw360.portal.portlets.projects.ProjectPortlet.java

License:Open Source License

private void exportExcel(ResourceRequest request, ResourceResponse response) {
    final User user = UserCacheHolder.getUserFromRequest(request);
    try {/*from   w ww  .  j ava  2s  .  c o  m*/
        ProjectService.Iface client = thriftClients.makeProjectClient();
        String searchText = request.getParameter(PortalConstants.KEY_SEARCH_TEXT);
        List<Project> projects;
        if (isNullOrEmpty(searchText)) {
            projects = client.getAccessibleProjectsSummary(user);
        } else {
            projects = client.searchByName(searchText, user);
        }

        ProjectExporter exporter = new ProjectExporter(thriftClients.makeComponentClient());
        PortletResponseUtil.sendFile(request, response, "Projects.xlsx", exporter.makeExcelExport(projects),
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    } catch (IOException | TException e) {
        log.error("An error occurred while generating the Excel export", e);
    }
}

From source file:org.eclipse.sw360.portal.common.AttachmentPortletUtils.java

License:Open Source License

private void serveAttachmentBundle(List<AttachmentContent> attachments, ResourceRequest request,
        ResourceResponse response, Optional<String> downloadFileName) {
    String filename;// w  w  w  .  j a v  a2  s .c  om
    String contentType;
    if (attachments.size() == 1) {
        filename = attachments.get(0).getFilename();
        contentType = attachments.get(0).getContentType();
        if (contentType.equalsIgnoreCase("application/gzip")) {
            // In case of downloads with gzip file extension (e.g. *.tar.gz)
            // the client should receive the origin file.
            // Set http header 'Content-Encoding' to 'identity'
            // to prevent auto encoding content (chrome).
            response.setProperty(HttpHeaders.CONTENT_ENCODING, "identity");
        }
    } else {
        filename = downloadFileName.orElse(DEFAULT_ATTACHMENT_BUNDLE_NAME);
        contentType = "application/zip";
    }

    User user = UserCacheHolder.getUserFromRequest(request);
    try {
        Optional<Object> context = getContextFromRequest(request, user);

        if (context.isPresent()) {
            try (InputStream attachmentStream = getStreamToServeAFile(attachments, user, context.get())) {
                PortletResponseUtil.sendFile(request, response, filename, attachmentStream, contentType);
            } catch (IOException e) {
                log.error("cannot finish writing response", e);
                response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
            }
        } else {
            log.warn("The user=[" + user.getEmail() + "] tried to download attachment=["
                    + CommonUtils.joinStrings(
                            attachments.stream().map(AttachmentContent::getId).collect(Collectors.toList()))
                    + "] in context=[" + request.getParameter(PortalConstants.CONTEXT_ID)
                    + "] without read permissions");
            response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "404");
        }
    } catch (SW360Exception e) {
        log.error("Context was not set properly.", e);
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "400");
    } catch (TException e) {
        log.error("Problem getting the attachment content from the backend", e);
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
    }
}

From source file:org.eclipse.sw360.portal.portlets.admin.UserPortlet.java

License:Open Source License

public void backUpUsers(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException, SystemException, PortalException {
    List<User> liferayUsers;
    try {/*from  w w w . j ava  2s.c  om*/
        liferayUsers = UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
    } catch (SystemException e) {
        log.error("Could not get user List from liferay", e);
        liferayUsers = Collections.emptyList();
    }

    final ByteArrayOutputStream outB = new ByteArrayOutputStream();
    Writer out = new BufferedWriter(new OutputStreamWriter(outB));

    CSVPrinter csvPrinter = new CSVPrinter(out, CommonUtils.sw360CsvFormat);

    csvPrinter.printRecord("GivenName", "Lastname", "Email", "Department", "UserGroup", "GID", "isMale",
            "PasswdHash", "wantsMailNotification");
    for (User liferayUser : liferayUsers) {

        String firstName = liferayUser.getFirstName();
        String lastName = liferayUser.getLastName();
        String emailAddress = liferayUser.getEmailAddress();
        List<Organization> organizations = liferayUser.getOrganizations();

        String department = "";

        if (organizations != null && organizations.size() > 0) {
            department = organizations.get(0).getName();
        }

        String gid = liferayUser.getOpenId();
        boolean isMale = liferayUser.isMale();
        String passwordHash = liferayUser.getPassword();
        if (isNullOrEmpty(emailAddress) || isNullOrEmpty(department)) {
            continue;
        }
        org.eclipse.sw360.datahandler.thrift.users.User sw360user = UserCacheHolder
                .getUserFromEmail(emailAddress);
        boolean wantsMailNotification = sw360user.isSetWantsMailNotification() ? sw360user.wantsMailNotification
                : true;
        String userGroup = sw360user.getUserGroup().toString();

        csvPrinter.printRecord(firstName, lastName, emailAddress, department, userGroup, gid, isMale,
                passwordHash, wantsMailNotification);
    }

    csvPrinter.flush();
    csvPrinter.close();

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outB.toByteArray());
    PortletResponseUtil.sendFile(request, response, "Users.csv", byteArrayInputStream, "text/csv");
}

From source file:org.eclipse.sw360.portal.portlets.admin.VendorPortlet.java

License:Open Source License

private void exportExcel(ResourceRequest request, ResourceResponse response) {
    try {//from   w ww.j a va  2s  .  c o m
        VendorService.Iface client = thriftClients.makeVendorClient();
        List<Vendor> vendors = client.getAllVendors();
        String filename = String.format("vendors-%s.xlsx", SW360Utils.getCreatedOn());
        PortletResponseUtil.sendFile(request, response, filename, exporter.makeExcelExport(vendors),
                CONTENT_TYPE_OPENXML_SPREADSHEET);
    } catch (IOException | TException e) {
        log.error("An error occurred while generating the Excel export", e);
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE,
                Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
    }
}