/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.search.web.internal.portlet;

import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.search.OpenSearch;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.search.PortalOpenSearchImpl;
import com.liferay.portal.search.web.constants.SearchPortletKeys;
import com.liferay.portal.search.web.internal.display.context.SearchDisplayContext;
import com.liferay.portal.search.web.internal.display.context.SearchDisplayContextFactory;

import java.io.IOException;

import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceURL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Eudaldo Alonso
 */
@Component(
	immediate = true,
	property = {
		"com.liferay.portlet.add-default-resource=true",
		"com.liferay.portlet.css-class-wrapper=portlet-search",
		"com.liferay.portlet.display-category=category.tools",
		"com.liferay.portlet.icon=/icons/search.png",
		"com.liferay.portlet.layout-cacheable=true",
		"com.liferay.portlet.preferences-owned-by-group=true",
		"com.liferay.portlet.private-request-attributes=false",
		"com.liferay.portlet.private-session-attributes=false",
		"com.liferay.portlet.restore-current-view=false",
		"com.liferay.portlet.use-default-template=true",
		"javax.portlet.display-name=Search", "javax.portlet.expiration-cache=0",
		"javax.portlet.init-param.template-path=/",
		"javax.portlet.init-param.view-template=/view.jsp",
		"javax.portlet.name=" + SearchPortletKeys.SEARCH,
		"javax.portlet.resource-bundle=content.Language",
		"javax.portlet.security-role-ref=guest,power-user,user",
		"javax.portlet.supports.mime-type=text/html"
	},
	service = Portlet.class
)
public class SearchPortlet extends MVCPortlet {

	@Override
	public void render(
			RenderRequest renderRequest, RenderResponse renderResponse)
		throws IOException, PortletException {

		SearchDisplayContext searchDisplayContext =
			searchDisplayContextFactory.create(
				renderRequest, renderResponse, renderRequest.getPreferences());

		renderRequest.setAttribute(
			SearchDisplayContext.class.getName(), searchDisplayContext);

		super.render(renderRequest, renderResponse);
	}

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

		String resourceID = GetterUtil.getString(
			resourceRequest.getResourceID());

		if (resourceID.equals("getOpenSearchXML")) {
			HttpServletRequest request = PortalUtil.getHttpServletRequest(
				resourceRequest);

			HttpServletResponse response = PortalUtil.getHttpServletResponse(
				resourceResponse);

			try {
				byte[] xml = getXML(resourceRequest, resourceResponse);

				ServletResponseUtil.sendFile(
					request, response, null, xml, ContentTypes.TEXT_XML_UTF8);
			}
			catch (Exception e) {
				try {
					PortalUtil.sendError(e, request, response);
				}
				catch (ServletException se) {
				}
			}
		}
		else {
			super.serveResource(resourceRequest, resourceResponse);
		}
	}

	protected byte[] getXML(
			ResourceRequest resourceRequest, ResourceResponse resourceResponse)
		throws Exception {

		ResourceURL openSearchResourceURL =
			resourceResponse.createResourceURL();

		openSearchResourceURL.setResourceID("getOpenSearchXML");

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

		ResourceURL openSearchDescriptionXMLURL =
			resourceResponse.createResourceURL();

		openSearchDescriptionXMLURL.setParameter(
			"mvcPath", "/open_search_description.jsp");
		openSearchDescriptionXMLURL.setParameter(
			"groupId", String.valueOf(groupId));

		OpenSearch openSearch = new PortalOpenSearchImpl(
			openSearchResourceURL.toString(),
			openSearchDescriptionXMLURL.toString());

		HttpServletRequest request = PortalUtil.getHttpServletRequest(
			resourceRequest);

		String xml = openSearch.search(
			request,
			openSearchResourceURL.toString() + StringPool.QUESTION +
				request.getQueryString());

		return xml.getBytes();
	}

	@Reference
	protected SearchDisplayContextFactory searchDisplayContextFactory;

}