001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.portlet;
016    
017    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
018    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
019    import com.liferay.portal.kernel.util.ContentTypes;
020    import com.liferay.portal.util.PortalUtil;
021    
022    import java.io.IOException;
023    
024    import javax.portlet.MimeResponse;
025    import javax.portlet.PortletRequest;
026    import javax.portlet.PortletResponse;
027    
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * @author Adolfo P??rez
033     */
034    public class JSONPortletResponseUtil {
035    
036            public static void writeJSON(
037                            PortletRequest portletRequest, MimeResponse mimeResponse,
038                            Object json)
039                    throws IOException {
040    
041                    mimeResponse.setContentType(_getContentType(portletRequest));
042    
043                    PortletResponseUtil.write(mimeResponse, json.toString());
044    
045                    mimeResponse.flushBuffer();
046            }
047    
048            public static void writeJSON(
049                            PortletRequest portletRequest, PortletResponse portletResponse,
050                            Object json)
051                    throws IOException {
052    
053                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
054                            portletResponse);
055    
056                    response.setContentType(_getContentType(portletRequest));
057    
058                    ServletResponseUtil.write(response, json.toString());
059    
060                    response.flushBuffer();
061            }
062    
063            private static String _getContentType(PortletRequest portletRequest) {
064                    String contentType = ContentTypes.APPLICATION_JSON;
065    
066                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
067                            portletRequest);
068    
069                    if (BrowserSnifferUtil.isIe(request)) {
070                            contentType = ContentTypes.TEXT_HTML;
071                    }
072    
073                    return contentType;
074            }
075    
076    }