|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2017 Intuit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *******************************************************************************/ |
| 16 | + |
| 17 | +package com.intuit.ia.connection; |
| 18 | + |
| 19 | +/** |
| 20 | + * |
| 21 | + * This class exposes APIs for disconnect (disconnect) and fetching blue dot menu dropdown(getAppMenu) |
| 22 | + */ |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import org.openid4java.discovery.Identifier; |
| 28 | + |
| 29 | +import com.intuit.ia.exception.ConnectionException; |
| 30 | +import com.intuit.ia.exception.OAuthException; |
| 31 | +import com.intuit.ia.exception.OpenIdException; |
| 32 | + |
| 33 | +public class IAPlatformClient { |
| 34 | + |
| 35 | + private PlatformHttpClient httpClient; |
| 36 | + |
| 37 | + private OAuthHelper oAuthHelper; |
| 38 | + private OpenIdHelper openIdHelper; |
| 39 | + |
| 40 | + public IAPlatformClient() { |
| 41 | + openIdHelper = new OpenIdHelper(); |
| 42 | + oAuthHelper = new OAuthHelper(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Disconnects the user from quickbooks |
| 47 | + * |
| 48 | + * @throws ConnectionException |
| 49 | + */ |
| 50 | + public PlatformResponse disconnect(String consumerKey, String consumerSecret, |
| 51 | + String accessToken, String accessTokenSecret) |
| 52 | + throws ConnectionException { |
| 53 | + httpClient = new PlatformHttpClient(consumerKey, consumerSecret, |
| 54 | + accessToken, accessTokenSecret); |
| 55 | + return this.httpClient.disconnect(); |
| 56 | + } |
| 57 | + /** |
| 58 | + * getCurrentUser the user from quickbooks |
| 59 | + * |
| 60 | + * @throws ConnectionException |
| 61 | + */ |
| 62 | + public User getcurrentUser(String consumerKey, String consumerSecret, |
| 63 | + String accessToken, String accessTokenSecret) |
| 64 | + throws ConnectionException { |
| 65 | + httpClient = new PlatformHttpClient(consumerKey, consumerSecret, |
| 66 | + accessToken, accessTokenSecret); |
| 67 | + User user = null;; |
| 68 | + try { |
| 69 | + user = this.httpClient.getCurrentUser(); |
| 70 | + } catch (Exception e) { |
| 71 | + e.printStackTrace(); |
| 72 | + } |
| 73 | + return user; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get App Menu returns list of all the applications that are linked with |
| 78 | + * the selected company |
| 79 | + * |
| 80 | + * @return List<String>: Returns HTML as a list of Strings |
| 81 | + * @throws ConnectionException |
| 82 | + * |
| 83 | + */ |
| 84 | + public List<String> getAppMenu(String consumerKey, String consumerSecret, |
| 85 | + String accessToken, String accessTokenSecret) |
| 86 | + throws ConnectionException { |
| 87 | + try { |
| 88 | + List<String> menulist; |
| 89 | + httpClient = new PlatformHttpClient(consumerKey, consumerSecret, |
| 90 | + accessToken, accessTokenSecret); |
| 91 | + menulist = this.httpClient.getAppMenu(); |
| 92 | + return menulist; |
| 93 | + } catch (ConnectionException conEx) { |
| 94 | + throw conEx; |
| 95 | + } catch (Exception e) { |
| 96 | + throw new ConnectionException("Failed to fetch appmenu: " |
| 97 | + + e.getMessage()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public Map<String, String> getRequestTokenAndSecret(String consumerKey, |
| 102 | + String consumerSecret) throws OAuthException { |
| 103 | + return oAuthHelper.getRequestToken(consumerKey, consumerSecret); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * |
| 108 | + * @param requestToken |
| 109 | + * @return authorizeUrl |
| 110 | + * @throws OAuthException |
| 111 | + * |
| 112 | + * This API will prepare the authorization URL and return it back |
| 113 | + */ |
| 114 | + |
| 115 | + public String getOauthAuthorizeUrl(String requestToken) |
| 116 | + throws OAuthException { |
| 117 | + |
| 118 | + return oAuthHelper.getAuthorizeUrl(requestToken); |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * |
| 124 | + * Gets the accesstoken and accesstokensecret |
| 125 | + * |
| 126 | + * @param verofierCode |
| 127 | + * @param requestToken |
| 128 | + * (After the user authorization) |
| 129 | + * @param requestTokensecret |
| 130 | + * @return Map<String> : where accesstoken will be in the key "accessToken" |
| 131 | + * and accesstokensecret will be in key "accessTokenSecret" |
| 132 | + * |
| 133 | + */ |
| 134 | + |
| 135 | + public Map<String, String> getOAuthAccessToken(String verifierCode, |
| 136 | + String requestToken, String requestTokenSecret, String consumerKey, |
| 137 | + String consumerSecret) throws OAuthException { |
| 138 | + |
| 139 | + return oAuthHelper.getAccessToken(verifierCode, requestToken, |
| 140 | + requestTokenSecret, consumerKey, consumerSecret); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Gets the authorization url for OpenId |
| 145 | + * |
| 146 | + * @throws |
| 147 | + * |
| 148 | + * |
| 149 | + */ |
| 150 | + |
| 151 | + public String getOpenIdAuthorizeUrl() throws OpenIdException { |
| 152 | + |
| 153 | + return openIdHelper.initOpenIdFlow(); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * |
| 158 | + * @param receivingUrl |
| 159 | + * @param parameterMap |
| 160 | + * @return Identifier : the OpenId Identifier |
| 161 | + * @throws OpenIdException |
| 162 | + */ |
| 163 | + public Identifier verifyOpenIdResponse(String receivingUrl, |
| 164 | + Map<String, String[]> parameterMap) throws OpenIdException { |
| 165 | + return openIdHelper.verifyResponse(receivingUrl, parameterMap); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * |
| 170 | + * @param consumerKey |
| 171 | + * @param consumerSecret |
| 172 | + * @param accessToken |
| 173 | + * @param accessTokenSecret |
| 174 | + * @throws ConnectionException |
| 175 | + */ |
| 176 | + public PlatformResponse reConnect(String consumerKey, String consumerSecret, |
| 177 | + String accessToken, String accessTokenSecret) |
| 178 | + throws ConnectionException { |
| 179 | + |
| 180 | + httpClient = new PlatformHttpClient(consumerKey, consumerSecret, |
| 181 | + accessToken, accessTokenSecret); |
| 182 | + return this.httpClient.reConnect(); |
| 183 | + } |
| 184 | +} |
0 commit comments