|
32 | 32 |
|
33 | 33 | import com.ibm.mq.jms.MQDestination; |
34 | 34 |
|
35 | | -import java.net.URI; |
36 | | -import java.net.http.HttpClient; |
37 | | -import java.net.http.HttpRequest; |
38 | | -import java.net.http.HttpRequest.BodyPublishers; |
39 | | -import java.net.http.HttpResponse; |
40 | | - |
41 | 35 | // Use these imports for building with Jakarta Messaging |
42 | 36 | // import jakarta.jms.Destination; |
43 | 37 | // import jakarta.jms.JMSConsumer; |
@@ -87,8 +81,8 @@ public static void main(String[] args) { |
87 | 81 | JmsConnectionFactory connectionFactory = createJMSConnectionFactory(); |
88 | 82 | setJMSProperties(connectionFactory); |
89 | 83 | logger.info("created connection factory"); |
90 | | - String access_token = obtainToken(); |
91 | | - context = connectionFactory.createContext(null , access_token); |
| 84 | + |
| 85 | + context = connectionFactory.createContext(); |
92 | 86 | logger.info("context created"); |
93 | 87 |
|
94 | 88 | // Set targetClient to be non JMS, so no JMS headers are transmitted. |
@@ -178,8 +172,8 @@ private static void setJMSProperties(JmsConnectionFactory cf) { |
178 | 172 | cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, APP_NAME); |
179 | 173 | if (null != APP_USER && !APP_USER.trim().isEmpty()) { |
180 | 174 | cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true); |
181 | | - // cf.setStringProperty(WMQConstants.USERID, APP_USER); |
182 | | - // cf.setStringProperty(WMQConstants.PASSWORD, APP_PASSWORD); |
| 175 | + cf.setStringProperty(WMQConstants.USERID, APP_USER); |
| 176 | + cf.setStringProperty(WMQConstants.PASSWORD, APP_PASSWORD); |
183 | 177 | } |
184 | 178 | if (CIPHER_SUITE != null && !CIPHER_SUITE.isEmpty()) { |
185 | 179 | cf.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, CIPHER_SUITE); |
@@ -219,40 +213,4 @@ private static void initialiseLogging() { |
219 | 213 | logger.finest("Logging initialised"); |
220 | 214 | } |
221 | 215 |
|
222 | | - public static String obtainToken() { |
223 | | - String access_token = ""; |
224 | | - String tokenEndpoint = System.getenv("JWT_TOKEN_ENDPOINT"); |
225 | | - String tokenUsername = System.getenv("JWT_TOKEN_USERNAME"); |
226 | | - String tokenPassword = System.getenv("JWT_TOKEN_PWD"); |
227 | | - String tokenClientId = System.getenv("JWT_TOKEN_CLIENTID"); |
228 | | - |
229 | | - //build the string with parameters provided as environment variables, to include in the POST request to the token issuer |
230 | | - String postBuild = String.format("client_id=%s&username=%s&password=%s&grant_type=password",tokenClientId, tokenUsername, tokenPassword); |
231 | | - logger.info("parameter string: " + postBuild); |
232 | | - HttpClient client = HttpClient.newHttpClient(); |
233 | | - |
234 | | - HttpRequest request = HttpRequest.newBuilder() |
235 | | - .uri(URI.create( |
236 | | - // "<keycloak URL>/realms/master/protocol/openid-connect/token" |
237 | | - tokenEndpoint |
238 | | - )) |
239 | | - .POST( |
240 | | - BodyPublishers.ofString(postBuild)) |
241 | | - .setHeader("Content-Type", "application/x-www-form-urlencoded") |
242 | | - .build(); |
243 | | - logger.info("obtaining token from:" + tokenEndpoint); |
244 | | - try { |
245 | | - HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
246 | | - logger.fine(String.valueOf(response.statusCode())); |
247 | | - logger.fine(response.body()); |
248 | | - |
249 | | - JSONObject myJsonObject = new JSONObject(response.body()); |
250 | | - access_token = myJsonObject.getString("access_token"); |
251 | | - logger.info("Using token:" + access_token); |
252 | | - } catch (Exception e) { |
253 | | - logger.info("Using token:" + access_token); |
254 | | - e.printStackTrace(); |
255 | | - } |
256 | | - return access_token; |
257 | | - } |
258 | 216 | } |
0 commit comments