/**
 * Copyright (c) 2000-2017 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * pSubscription 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.portlet.portalsettings.action;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.security.auth.AuthTokenUtil;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.util.PortalUtil;

import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * @author Tibor Lipusz
 */
public class TestLDAPConnectionAction extends PortletAction {

	@Override
	public ActionForward render(
		ActionMapping actionMapping, ActionForm actionForm,
		PortletConfig portletConfig, RenderRequest renderRequest,
		RenderResponse renderResponse)
	throws Exception {

		try {
			AuthTokenUtil.checkCSRFToken(
				PortalUtil.getOriginalServletRequest(
					PortalUtil.getHttpServletRequest(renderRequest)),
				getClass().getName());

			return actionMapping.findForward(
				getForward(
					renderRequest,
					"portlet.portal_settings.test_ldap_connection"));
		}
		catch (PrincipalException pe) {
			if (_log.isDebugEnabled()) {
				_log.debug(
					"Unable to test LDAP connection: " + pe.getMessage(), pe);
			}
			else if (_log.isWarnEnabled()) {
				_log.warn("Unable to test LDAP connection: " + pe.getMessage());
			}

			return actionMapping.findForward(
				getForward(renderRequest, "portlet.portal_settings.error"));
		}
	}

	private static Log _log = LogFactoryUtil.getLog(
		TestLDAPConnectionAction.class);

}