001
014
015 package com.liferay.portlet.translator;
016
017 import com.liferay.portal.kernel.microsofttranslator.MicrosoftTranslatorException;
018 import com.liferay.portal.kernel.servlet.SessionErrors;
019 import com.liferay.portal.kernel.util.ParamUtil;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.kernel.webcache.WebCacheException;
022 import com.liferay.portal.util.WebKeys;
023 import com.liferay.portlet.translator.model.Translation;
024 import com.liferay.portlet.translator.util.TranslatorUtil;
025 import com.liferay.util.bridges.mvc.MVCPortlet;
026
027 import javax.portlet.ActionRequest;
028 import javax.portlet.ActionResponse;
029 import javax.portlet.PortletException;
030
031
034 public class TranslatorPortlet extends MVCPortlet {
035
036 @Override
037 public void processAction(
038 ActionRequest actionRequest, ActionResponse actionResponse)
039 throws PortletException {
040
041 try {
042 String translationId = ParamUtil.getString(actionRequest, "id");
043 String fromText = ParamUtil.getString(actionRequest, "text");
044
045 if (Validator.isNotNull(fromText)) {
046 Translation translation = TranslatorUtil.getTranslation(
047 translationId, fromText);
048
049 actionRequest.setAttribute(
050 WebKeys.TRANSLATOR_TRANSLATION, translation);
051 }
052 }
053 catch (WebCacheException wce) {
054 Throwable cause = wce.getCause();
055
056 if (cause instanceof MicrosoftTranslatorException) {
057 SessionErrors.add(actionRequest, cause.getClass(), cause);
058 }
059 else {
060 throw new PortletException(wce);
061 }
062 }
063 catch (Exception e) {
064 throw new PortletException(e);
065 }
066 }
067
068 }