001    /**
002     * Copyright (c) 2000-2012 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.microsofttranslator;
016    
017    import com.liferay.portal.kernel.microsofttranslator.MicrosoftTranslator;
018    import com.liferay.portal.kernel.microsofttranslator.MicrosoftTranslatorException;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.Http;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    /**
027     * @author Hugo Huijser
028     */
029    public class MicrosoftTranslatorImpl implements MicrosoftTranslator {
030    
031            public MicrosoftTranslatorImpl() {
032                    _microsoftTranslatorAuthenticator =
033                            new MicrosoftTranslatorAuthenticator();
034            }
035    
036            public MicrosoftTranslatorImpl(String clientId, String clientSecret) {
037                    _microsoftTranslatorAuthenticator =
038                            new MicrosoftTranslatorAuthenticator(clientId, clientSecret);
039            }
040    
041            public MicrosoftTranslatorAuthenticator
042                    getMicrosoftTranslatorAuthenticator() {
043    
044                    return _microsoftTranslatorAuthenticator;
045            }
046    
047            public String translate(
048                            String fromLanguageId, String toLanguageId, String fromText)
049                    throws MicrosoftTranslatorException {
050    
051                    try {
052                            return doTranslate(fromLanguageId, toLanguageId, fromText);
053                    }
054                    catch (MicrosoftTranslatorException mte) {
055                            throw mte;
056                    }
057                    catch (Exception e) {
058                            throw new MicrosoftTranslatorException(e);
059                    }
060            }
061    
062            protected String doTranslate(
063                            String fromLanguageId, String toLanguageId, String fromText)
064                    throws Exception {
065    
066                    fromLanguageId = getMicrosoftLanguageId(fromLanguageId);
067                    toLanguageId = getMicrosoftLanguageId(toLanguageId);
068    
069                    Http.Options options = new Http.Options();
070    
071                    StringBundler sb = new StringBundler(7);
072    
073                    sb.append("http://api.microsofttranslator.com/v2/Http.svc/Translate?");
074                    sb.append("text=");
075                    sb.append(HttpUtil.encodeURL(fromText));
076                    sb.append("&from=");
077                    sb.append(fromLanguageId);
078                    sb.append("&to=");
079                    sb.append(toLanguageId);
080    
081                    options.setLocation(sb.toString());
082    
083                    String accessToken = _microsoftTranslatorAuthenticator.getAccessToken();
084    
085                    if (Validator.isNull(accessToken)) {
086                            throw new MicrosoftTranslatorException(
087                                    _microsoftTranslatorAuthenticator.getError());
088                    }
089    
090                    options.addHeader("Authorization", "Bearer " + accessToken);
091    
092                    String text = HttpUtil.URLtoString(options);
093    
094                    int x = text.indexOf(">") + 1;
095                    int y = text.indexOf("</string>", x);
096    
097                    if ((x == -1) || (y == -1)) {
098                            x = text.indexOf("Message: ");
099                            y = text.indexOf("<", x);
100    
101                            if ((x > -1) && (y > -1)) {
102                                    text = text.substring(x, y);
103                            }
104    
105                            throw new MicrosoftTranslatorException(text);
106                    }
107    
108                    String toText = text.substring(x, y);
109    
110                    toText = toText.trim();
111    
112                    return StringUtil.replace(toText, CharPool.NEW_LINE, CharPool.SPACE);
113            }
114    
115            protected String getMicrosoftLanguageId(String languageId) {
116                    if (languageId.equals("pt_BR") || languageId.equals("pt_PT")) {
117                            return "pt";
118                    }
119                    else if (languageId.equals("hi_IN")) {
120                            return "hi";
121                    }
122                    else if (languageId.equals("in")) {
123                            return "id";
124                    }
125                    else if (languageId.equals("iw")) {
126                            return "he";
127                    }
128                    else if (languageId.equals("nb")) {
129                            return "no";
130                    }
131                    else if (languageId.equals("zh_CN")) {
132                            return "zh-CHS";
133                    }
134                    else if (languageId.equals("zh_TW")) {
135                            return "zh-CHT";
136                    }
137    
138                    return languageId;
139            }
140    
141            private MicrosoftTranslatorAuthenticator _microsoftTranslatorAuthenticator;
142    
143    }