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.fmdp.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);//ww  w .  j  a va 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);

    }
    fieldLabels.add("email-from");
    sb.append(getCSVFormattedValue("email-from"));
    sb.append(PortletPropsValues.CSV_SEPARATOR);

    fieldLabels.add("email-sent-on");
    sb.append(getCSVFormattedValue("email-sent-on"));
    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.liferay.calendar.portlet.CalendarPortlet.java

License:Open Source License

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

    if (!PortalUtil.isRSSFeedsEnabled()) {
        PortalUtil.sendRSSFeedsDisabledError(resourceRequest, resourceResponse);

        return;// w w  w  . j  a  v  a  2s  . c  o  m
    }

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

    long calendarId = ParamUtil.getLong(resourceRequest, "calendarId");

    PortletPreferences portletPreferences = resourceRequest.getPreferences();

    long timeInterval = GetterUtil.getLong(portletPreferences.getValue("rssTimeInterval", StringPool.BLANK),
            RSSUtil.TIME_INTERVAL_DEFAULT);

    long startTime = System.currentTimeMillis();

    long endTime = startTime + timeInterval;

    int max = GetterUtil.getInteger(portletPreferences.getValue("rssDelta", StringPool.BLANK),
            SearchContainer.DEFAULT_DELTA);
    String rssFeedType = portletPreferences.getValue("rssFeedType", RSSUtil.FORMAT_DEFAULT);
    String type = RSSUtil.getFormatType(rssFeedType);
    double version = RSSUtil.getFeedTypeVersion(rssFeedType);
    String displayStyle = portletPreferences.getValue("rssDisplayStyle", RSSUtil.DISPLAY_STYLE_DEFAULT);

    String rss = CalendarBookingServiceUtil.getCalendarBookingsRSS(calendarId, startTime, endTime, max, type,
            version, displayStyle, themeDisplay);

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, null, rss.getBytes(),
            ContentTypes.TEXT_XML_UTF8);
}

From source file:com.liferay.calendar.portlet.CalendarPortlet.java

License:Open Source License

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

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

    long calendarId = ParamUtil.getLong(resourceRequest, "calendarId");

    Calendar calendar = CalendarLocalServiceUtil.getCalendar(calendarId);

    String fileName = calendar.getName(themeDisplay.getLocale()) + CharPool.PERIOD
            + String.valueOf(CalendarDataFormat.ICAL);

    CalendarDataHandler calendarDataHandler = CalendarDataHandlerFactory
            .getCalendarDataHandler(CalendarDataFormat.ICAL);

    String data = calendarDataHandler.exportCalendar(calendarId);

    String contentType = MimeTypesUtil.getContentType(fileName);

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

From source file:com.liferay.calendar.web.internal.portlet.CalendarPortlet.java

License:Open Source License

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

    PortletPreferences portletPreferences = resourceRequest.getPreferences();

    boolean enableRss = GetterUtil.getBoolean(portletPreferences.getValue("enableRss", null), true);

    if (!_portal.isRSSFeedsEnabled() || !enableRss) {
        _portal.sendRSSFeedsDisabledError(resourceRequest, resourceResponse);

        return;//from  ww w  .j a  v a 2s.  c o  m
    }

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

    long calendarId = ParamUtil.getLong(resourceRequest, "calendarId");

    long timeInterval = GetterUtil.getLong(portletPreferences.getValue("rssTimeInterval", StringPool.BLANK),
            RSSUtil.TIME_INTERVAL_DEFAULT);

    long startTime = System.currentTimeMillis();

    long endTime = startTime + timeInterval;

    int max = GetterUtil.getInteger(portletPreferences.getValue("rssDelta", StringPool.BLANK),
            SearchContainer.DEFAULT_DELTA);

    String rssFeedType = portletPreferences.getValue("rssFeedType", RSSUtil.FORMAT_DEFAULT);

    String type = RSSUtil.getFormatType(rssFeedType);
    double version = RSSUtil.getFeedTypeVersion(rssFeedType);

    String displayStyle = portletPreferences.getValue("rssDisplayStyle", RSSUtil.DISPLAY_STYLE_DEFAULT);

    String rss = _calendarBookingService.getCalendarBookingsRSS(calendarId, startTime, endTime, max, type,
            version, displayStyle, themeDisplay);

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, null, rss.getBytes(),
            ContentTypes.TEXT_XML_UTF8);
}

From source file:com.liferay.calendar.web.internal.portlet.CalendarPortlet.java

License:Open Source License

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

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

    long calendarId = ParamUtil.getLong(resourceRequest, "calendarId");

    Calendar calendar = _calendarService.getCalendar(calendarId);

    String fileName = calendar.getName(themeDisplay.getLocale()) + CharPool.PERIOD
            + String.valueOf(CalendarDataFormat.ICAL);

    CalendarDataHandler calendarDataHandler = CalendarDataHandlerFactory
            .getCalendarDataHandler(CalendarDataFormat.ICAL);

    String data = calendarDataHandler.exportCalendar(calendarId);

    String contentType = MimeTypesUtil.getContentType(fileName);

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

From source file:com.liferay.configuration.admin.web.internal.portlet.action.ExportConfigurationMVCResourceCommand.java

License:Open Source License

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

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

    String languageId = themeDisplay.getLanguageId();

    ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();

    Map<String, ConfigurationModel> configurationModels = _configurationModelRetriever
            .getConfigurationModels(themeDisplay.getLanguageId());

    for (ConfigurationModel configurationModel : configurationModels.values()) {

        if (configurationModel.isFactory()) {
            String curFactoryPid = configurationModel.getFactoryPid();

            List<ConfigurationModel> factoryInstances = _configurationModelRetriever
                    .getFactoryInstances(configurationModel);

            for (ConfigurationModel factoryInstance : factoryInstances) {
                String curPid = factoryInstance.getID();

                String curFileName = getFileName(curFactoryPid, curPid);

                zipWriter.addEntry(curFileName, ConfigurationExporter
                        .getPropertiesAsBytes(getProperties(languageId, curFactoryPid, curPid)));
            }/*from w w  w  .j a  va 2s. co  m*/
        } else if (configurationModel.hasConfiguration()) {
            String curPid = configurationModel.getID();

            String curFileName = getFileName(null, curPid);

            zipWriter.addEntry(curFileName,
                    ConfigurationExporter.getPropertiesAsBytes(getProperties(languageId, curPid, curPid)));
        }
    }

    String fileName = "liferay-system-settings.zip";

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, fileName,
            new FileInputStream(zipWriter.getFile()), ContentTypes.APPLICATION_ZIP);
}

From source file:com.liferay.configuration.admin.web.internal.portlet.action.ExportConfigurationMVCResourceCommand.java

License:Open Source License

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

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

    String languageId = themeDisplay.getLanguageId();

    ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();

    String factoryPid = ParamUtil.getString(resourceRequest, "factoryPid");

    Map<String, ConfigurationModel> configurationModels = _configurationModelRetriever
            .getConfigurationModels(themeDisplay.getLanguageId());

    ConfigurationModel factoryConfigurationModel = configurationModels.get(factoryPid);

    List<ConfigurationModel> factoryInstances = _configurationModelRetriever
            .getFactoryInstances(factoryConfigurationModel);

    for (ConfigurationModel factoryInstance : factoryInstances) {
        String curPid = factoryInstance.getID();

        String curFileName = getFileName(factoryPid, curPid);

        zipWriter.addEntry(curFileName,/*  w ww.  j  a  va 2s.co m*/
                ConfigurationExporter.getPropertiesAsBytes(getProperties(languageId, factoryPid, curPid)));
    }

    String fileName = "liferay-system-settings-" + factoryConfigurationModel.getFactoryPid() + ".zip";

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, fileName,
            new FileInputStream(zipWriter.getFile()), ContentTypes.APPLICATION_ZIP);
}

From source file:com.liferay.configuration.admin.web.internal.portlet.action.ExportConfigurationMVCResourceCommand.java

License:Open Source License

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

    String factoryPid = ParamUtil.getString(resourceRequest, "factoryPid");
    String pid = ParamUtil.getString(resourceRequest, "pid");

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

    String languageId = themeDisplay.getLanguageId();

    String fileName = getFileName(factoryPid, pid);

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, fileName,
            ConfigurationExporter.getPropertiesAsBytes(getProperties(languageId, factoryPid, pid)),
            ContentTypes.TEXT_XML_UTF8);
}

From source file:com.liferay.contacts.contactscenter.portlet.ContactsCenterPortlet.java

License:Open Source License

public void exportVCard(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception {

    long userId = ParamUtil.getLong(resourceRequest, "userId");

    User user = UserServiceUtil.getUserById(userId);

    String vCard = ContactsUtil.getVCard(user);

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, user.getFullName() + ".vcf",
            vCard.getBytes(StringPool.UTF8), "text/x-vcard; charset=UTF-8");
}

From source file:com.liferay.contacts.contactscenter.portlet.ContactsCenterPortlet.java

License:Open Source License

public void exportVCards(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception {

    long[] userIds = StringUtil.split(ParamUtil.getString(resourceRequest, "userIds"), 0L);

    List<User> users = new ArrayList<User>(userIds.length);

    for (long userId : userIds) {
        User user = UserServiceUtil.getUserById(userId);

        users.add(user);//from   ww  w.  j a  va  2s. c  o m
    }

    String vCards = ContactsUtil.getVCards(users);

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, "vcards.vcf",
            vCards.getBytes(StringPool.UTF8), "text/x-vcard; charset=UTF-8");
}