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