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 fromLanguageId = ParamUtil.getString(
043 actionRequest, "fromLanguageId");
044 String toLanguageId = ParamUtil.getString(
045 actionRequest, "toLanguageId");
046 String fromText = ParamUtil.getString(actionRequest, "text");
047
048 if (Validator.isNotNull(fromText)) {
049 Translation translation = TranslatorUtil.getTranslation(
050 fromLanguageId, toLanguageId, fromText);
051
052 actionRequest.setAttribute(
053 WebKeys.TRANSLATOR_TRANSLATION, translation);
054 }
055 }
056 catch (WebCacheException wce) {
057 Throwable cause = wce.getCause();
058
059 if (cause instanceof MicrosoftTranslatorException) {
060 SessionErrors.add(actionRequest, cause.getClass(), cause);
061 }
062 else {
063 throw new PortletException(wce);
064 }
065 }
066 catch (Exception e) {
067 throw new PortletException(e);
068 }
069 }
070
071 }