orpiske commented on code in PR #11628: URL: https://github.com/apache/camel/pull/11628#discussion_r1343069732
########## components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java: ########## @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.http; + +import org.apache.camel.util.json.DeserializationException; +import org.apache.camel.util.json.JsonObject; +import org.apache.camel.util.json.Jsoner; +import org.apache.hc.client5.http.classic.HttpClient; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.EntityDetails; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OAuth2ClientConfigurer implements HttpClientConfigurer { + + private static final Logger LOG = LoggerFactory.getLogger(OAuth2ClientConfigurer.class); + + private final String clientId; + private final String clientSecret; + private final String tokenEndpoint; + + public OAuth2ClientConfigurer(String clientId, String clientSecret, String tokenEndpoint) { + this.clientId = clientId; + this.clientSecret = clientSecret; + this.tokenEndpoint = tokenEndpoint; + } + + @Override + public void configureHttpClient(HttpClientBuilder clientBuilder) { + HttpClient httpClient = clientBuilder.build(); + clientBuilder.addRequestInterceptorFirst((HttpRequest request, EntityDetails entity, HttpContext context) -> { + + final HttpPost httpPost = new HttpPost(tokenEndpoint); + + httpPost.addHeader(HttpHeaders.AUTHORIZATION, + HttpCredentialsHelper.generateBasicAuthHeader(clientId, clientSecret)); + httpPost.setEntity(new StringEntity("grant_type=client_credentials", ContentType.APPLICATION_FORM_URLENCODED)); + + httpClient.execute(httpPost, response -> { + + try { + String responseString = EntityUtils.toString(response.getEntity()); + + if (response.getCode() == 200) { + String accessToken = ((JsonObject) Jsoner.deserialize(responseString)).getString("access_token"); + request.addHeader(HttpHeaders.AUTHORIZATION, accessToken); + } else { + // throw exception?? For that, needs to change HttpClientConfigurer interface to allow it + } + + } catch (DeserializationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + }); Review Comment: As we discussed via chat, this part seems to be one of interest. We might want to have some flexibility about how to handle the exceptions here but, also, be able to avoid unnecessary overhead when it's not necessary. I think it would be interesting to get some insights from @davsclaus, @oscerd and others about what they think we should do here. ########## components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java: ########## @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.http; + +import org.apache.camel.util.json.DeserializationException; +import org.apache.camel.util.json.JsonObject; +import org.apache.camel.util.json.Jsoner; +import org.apache.hc.client5.http.classic.HttpClient; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.EntityDetails; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OAuth2ClientConfigurer implements HttpClientConfigurer { + + private static final Logger LOG = LoggerFactory.getLogger(OAuth2ClientConfigurer.class); + + private final String clientId; + private final String clientSecret; + private final String tokenEndpoint; + + public OAuth2ClientConfigurer(String clientId, String clientSecret, String tokenEndpoint) { + this.clientId = clientId; + this.clientSecret = clientSecret; + this.tokenEndpoint = tokenEndpoint; + } + + @Override + public void configureHttpClient(HttpClientBuilder clientBuilder) { + HttpClient httpClient = clientBuilder.build(); + clientBuilder.addRequestInterceptorFirst((HttpRequest request, EntityDetails entity, HttpContext context) -> { + + final HttpPost httpPost = new HttpPost(tokenEndpoint); + + httpPost.addHeader(HttpHeaders.AUTHORIZATION, + HttpCredentialsHelper.generateBasicAuthHeader(clientId, clientSecret)); + httpPost.setEntity(new StringEntity("grant_type=client_credentials", ContentType.APPLICATION_FORM_URLENCODED)); + + httpClient.execute(httpPost, response -> { + + try { + String responseString = EntityUtils.toString(response.getEntity()); + + if (response.getCode() == 200) { + String accessToken = ((JsonObject) Jsoner.deserialize(responseString)).getString("access_token"); + request.addHeader(HttpHeaders.AUTHORIZATION, accessToken); + } else { + // throw exception?? For that, needs to change HttpClientConfigurer interface to allow it + } + + } catch (DeserializationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + }); Review Comment: As we discussed via chat, this part seems to be one of interest. We might want to have some flexibility about how to handle the exceptions here but, also, be able to avoid unnecessary overhead when it's not necessary. I think it would be interesting to get some insights from @davsclaus, @oscerd and others about what they think you should do here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org