javaspring-bootaws-lambdaaws-sdkalfresco-maven

Check the Alfresco Endpoint URL is available or not in Java


Hi I have created a Lambda function and trying to check one of my endpoint URL(Alfresco process) is available or not while checking the URL availability the code perfectly working for http URL not for HTTP'S.

My requirement is i have to send the mail using SES if Endpoint URL is not available while checking the availability i'am getting below error for Https URL's

Error : java.net.ConnectException: Connection refused:connect

Here is my sample code


package com.Firstlambda;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
import com.amazonaws.services.simpleemail.model.*;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class test {
    public static void main(String[] args) throws IOException {

        BasicAWSCredentials creds = new BasicAWSCredentials("xxxxxxx",
                "xxxxxxxx");

        final String FROM = "xxxxxx";
        final String TO = "xxxxxx";
//        final String CONFIGSET = "xxxxx";
        final String SUBJECT = "Amazon SES test (AWS SDK for Java)";


        final String HTMLBODY = "<h1>Amazon SES test (AWS SDK for Java)</h1>"
                + "<p>This email was sent with <a href='https://aws.amazon.com/ses/'>"
                + "Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-java/'>"
                + "AWS SDK for Java</a>";


        final String TEXTBODY = "This email was sent through Amazon SES "
                + "using the AWS SDK for Java.";

        //String GetStaus = getStatus("https://www.youtube.com");
        String GetStaus = getStatus("http://localhost:9020/OCR/test");

        if (GetStaus.toLowerCase().equals("green")) {
            AmazonSimpleEmailService client =
                    AmazonSimpleEmailServiceClientBuilder.standard()
                            .withCredentials(new AWSStaticCredentialsProvider(creds))
                            .withRegion(Regions.US_EAST_1).build();

            SendEmailRequest request = new SendEmailRequest()
                    .withDestination(
                            new Destination().withToAddresses(TO))
                    .withMessage(new Message()
                            .withBody(new Body()
                                    .withHtml(new Content()
                                            .withCharset("UTF-8").withData(HTMLBODY))
                                    .withText(new Content()
                                            .withCharset("UTF-8").withData(TEXTBODY)))
                            .withSubject(new Content()
                                    .withCharset("UTF-8").withData(SUBJECT)))
                    .withSource(FROM);
            client.sendEmail(request);
        }
        System.out.println("Email sent!");
    }


    public static String getStatus(String url) throws IOException {

        String result = "";
        try {
            URL siteURL = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) siteURL
                    .openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            int code = connection.getResponseCode();
            if (code == 200) {
                result = "Green";
            }
        } catch (Exception e) {
            result = "->Red<-";
        }
        return result;
    }
}

Could anyone help me how to check the availability of http's URL in my Spring boot java application? If resolved i have to use the same in my java service too. Thanks in advance

As requested i have placed my entire stackrace

java.net.ConnectException: Connection refused: connect
    at java.base/java.net.PlainSocketImpl.connect0(Native Method)
    at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:101)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    at java.base/java.net.Socket.connect(Socket.java:591)
    at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:285)
    at java.base/sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
    at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182)
    at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
    at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
    at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:265)
    at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:372)
    at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075)
    at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:163)
    at com.Firstlambda.test.getStatus(test.java:70)
    at com.Firstlambda.test.main(test.java:35)


Solution

  • Above code working fine. It is a proxy issue of my organization. If anyone face same issue kindly contact admin team of your organization to check if there any firewall issue