CloseableHttpClient can't execute in AEM
Hi
I created a AEM servlet to call a external API and I tried to test it on local, my problem is that the CloseableHttpClient can not be executed.
@SlingServlet(paths={"/bin/test01"},methods= {"POST"},metatype=false)
public class OauthAuthServlet extends SlingAllMethodsServlet {
/**
*
*/
private static final long serialVersionUID = -4325654356300019990L;
private final Logger log = LoggerFactory.getLogger(OauthAuthServlet.class);
CloseableHttpClient httpPostClient = HttpClients.createDefault();
public static String OAUTH_SERVER_URL = "https://dm-us.informaticacloud.com/authz-service/oauth/token";
@Override
protected void doGet( SlingHttpServletRequest request, SlingHttpServletResponse response) {
this.doPost(request, response);
}
@Override
protected void doPost( SlingHttpServletRequest request, SlingHttpServletResponse response){
String cc= request.getParameter("CLIENT_CREDENTIALS");
String token = null;
setLocalHttpProxy();
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(OAUTH_SERVER_URL);
String header = "Basic "+cc;
httpPost.addHeader("Authorization", header);
List<NameValuePair> formparams = new ArrayList<>();
formparams.add(new BasicNameValuePair("grant_type","client_credentials"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(formparams));
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();
httpPost.setConfig(requestConfig);
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
try {
HttpEntity entity = httpResponse.getEntity();
token=EntityUtils.toString(entity);
}catch(Exception e) {
e.printStackTrace();
}finally {
httpResponse.close();
}
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
}catch(Exception e) {
e.printStackTrace();
}
}
try {
response.getWriter().write(token);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The servlet always skip this line "
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);", I don't see any errors pop up in logs.
Please give me some advises, thank you.