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.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.ContentTypes;
022    import com.liferay.portal.kernel.util.Http;
023    import com.liferay.portal.kernel.util.HttpUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Time;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.util.PropsValues;
029    
030    /**
031     * @author Hugo Huijser
032     */
033    public class MicrosoftTranslatorAuthenticator {
034    
035            public MicrosoftTranslatorAuthenticator() {
036                    init(true);
037            }
038    
039            public MicrosoftTranslatorAuthenticator(
040                    String clientId, String clientSecret) {
041    
042                    _clientId = clientId;
043                    _clientSecret = clientSecret;
044    
045                    init(true);
046            }
047    
048            public String getAccessToken() {
049                    init(false);
050    
051                    return _accessToken;
052            }
053    
054            public String getError() {
055                    return _error;
056            }
057    
058            public void init(boolean manual) {
059                    if (manual || isStale()) {
060                            doInit();
061                    }
062            }
063    
064            protected void doInit() {
065                    if (Validator.isNull(_clientId)) {
066                            _clientId = PropsValues.MICROSOFT_TRANSLATOR_CLIENT_ID;
067                            _clientSecret = PropsValues.MICROSOFT_TRANSLATOR_CLIENT_SECRET;
068                    }
069    
070                    try {
071                            Http.Options options = new Http.Options();
072    
073                            StringBundler sb = new StringBundler(5);
074    
075                            sb.append("grant_type=client_credentials&client_id=");
076                            sb.append(HttpUtil.encodeURL(_clientId));
077                            sb.append("&client_secret=");
078                            sb.append(HttpUtil.encodeURL(_clientSecret));
079                            sb.append("&scope=http://api.microsofttranslator.com");
080    
081                            options.setBody(
082                                    sb.toString(), ContentTypes.APPLICATION_X_WWW_FORM_URLENCODED,
083                                    StringPool.UTF8);
084    
085                            options.setLocation(_URL);
086    
087                            options.setPost(true);
088    
089                            String jsonString = HttpUtil.URLtoString(options);
090    
091                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
092                                    jsonString);
093    
094                            _error = jsonObject.getString("error_description");
095    
096                            if (_error != null) {
097                                    if (_log.isInfoEnabled()) {
098                                            _log.info("Unable to initialize access token: " + _error);
099                                    }
100                            }
101    
102                            _accessToken = jsonObject.getString("access_token");
103    
104                            if (_accessToken != null) {
105                                    _log.info("Access token " + _accessToken);
106                            }
107    
108                            _initTime = System.currentTimeMillis();
109                    }
110                    catch (Exception e) {
111                            if (_log.isInfoEnabled()) {
112                                    _log.info("Unable to initialize authentication token", e);
113                            }
114                    }
115            }
116    
117            protected boolean isStale() {
118                    if ((_initTime + _EXPIRE_TIME) > System.currentTimeMillis()) {
119                            return false;
120                    }
121                    else {
122                            return true;
123                    }
124            }
125    
126            private static final long _EXPIRE_TIME = 10 * Time.MINUTE;
127    
128            private static final String _URL =
129                    "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
130    
131            private static Log _log = LogFactoryUtil.getLog(
132                    MicrosoftTranslatorAuthenticator.class);
133    
134            private String _accessToken;
135            private String _clientId;
136            private String _clientSecret;
137            private String _error;
138            private long _initTime;
139    
140    }