Java Posts

Does JBoss EAP’s “org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker” doing a “Select 1 from Dual”?

TL;DR
No it is NOT.

Longer Version :
JBoss EAP and Wildfly have its own internal query validator to check whether connection is active or not before doing connection to Oracle database, it resides on below class.

org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker

Why i’m interested in OracleValidConnectionChecker class? Because several days ago, i got a critical question from Habiburrokhman Sjarbini, my fellow RedHat Platform Consultant regarding this. Whether OracleValidConnectionChecker which is embedded in JBoss EAP will do a “select 1 from dual” query?

I have to decompile a lot of classes to see what is running under the hood,

Here is the screenshot of what OracleValidConnectionChecker looks like,

On Oracle database, it will call “oracle.jdbc.driver.OracleConnection” and invoke method “pingDatabase”. Lets check what is the content of “pingDatabase” method,

OracleConnection will call its super class, OracleConnectionWrapper, and invoke pingDatabase method. It goes here and there, until finally it reach oracle.jdbc.driver.PhysicalConnection class on method doPingDatabase.

And as you can see, it runs a “SELECT ‘x’ FROM DUAL” query, instead on “select 1 from dual”. :)

Jar versions :

ojdbc8.jar
ironjacamar-jdbc-1.4.11.Final.jar
Google+

Removing Bucket Name as Subdomain when Using AmazonS3Client Android Library

On a simple java desktop app, uploading an image to an S3-compliant API service is so simple. Each bucket are represented as folders, therefore no special approach is needed. Here is how it was done,

public AWSTestCase() {
	AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration("http://localhost:8082/", "us-west-2");
	client = AmazonS3ClientBuilder.standard()
			.withPathStyleAccessEnabled(true)
			.withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()))
			.withEndpointConfiguration(endpoint)
			.withClientConfiguration(new ClientConfiguration().withProtocol(Protocol.HTTP))
			.enablePathStyleAccess()
			.build();
}

@Test
public void testUploadToBucket() {
	client.putObject("bucket01", "jim.png", new File("d:\\jim.png"));
}

It will try to upload to below url,

http://localhost:8082/bucket01

But the same approach is not working on Android,

File file = new File(Environment.getExternalStorageDirectory(), attachmentModel.getLocalPath());

AWSCredentials credentials = new BasicAWSCredentials(ConstantCommon.ACCESS_KEY, ConstantCommon.SECRET_KEY);
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTP);

AmazonS3Client client = new AmazonS3Client(credentials, clientConfig);
client.setEndpoint("http://localhost:8082/");
client.putObject("bucket01", attachmentModel.getRemotePath(), file);

It will gives error,

com.amazonaws.AmazonClientException: Unable to execute HTTP request: bucket01.localhost

Somehow app will try to upload to below url, because bucket name are treated as subdomain.

http://bucket01.localhost:8082/

Workaround is quite easy, setting bucket name as empty string and set bucket name as folder on endpoint should solve this issue.

public AWSTestCase() {
	client = new AmazonS3Client(new AnonymousAWSCredentials());
	client.setEndpoint("http://localhost:8082/bucket01");
}

@Test
public void testUploadToBucket() {
	client.putObject("", "jim.png", new File("d:\\jim.png"));
}

The content of my Gradle file,

    implementation 'com.amazonaws:aws-android-sdk-s3:2.9.2'
    implementation 'com.amazonaws:aws-android-sdk-core:2.16.5'
Google+

Slowness when Generating SecureRandom with Java on RHEL 7

Today i had a very weird issue where there is slowness on my app when generating random secure number on my server, the weird thing is that it never happen on my Windows machine. After intensive debugging i found out that below code part is causing the slowness,

    static {
        try {
            secureRandom = SecureRandom.getInstanceStrong();
        } catch (Exception e) {
            secureRandom = new SecureRandom();
        }
    }

Replacing it with below code solve my problem,

    static {
        try {
            secureRandom = SecureRandom.getInstance("SHA1PRNG");
        } catch (Exception e) {
            secureRandom = new SecureRandom();
        }
    }
Google+

How to Connect and Uploading Files to Openstack using Java

Currently im involved in a project where i have to use OpenStack Object Store for storing images and files. After Googling for a while, i come up with a solution of using JOSS library to connect to OpenStack, despite OpenStack provide REST API to connect to it directly.

But first, we need to see the API URL that is needed for login to OpenStack. And it can be seen on below screenshot, inside the red box.

After we got the url, next is importing JOSS library to be used in our app. We can import it by using a simple maven command,

<dependency>
	<groupId>org.javaswift</groupId>
	<artifactId>joss</artifactId>
	<version>0.10.2</version>
</dependency>

And a simple java file, to do a simple upload using a provided credential.

@Test
public void uploadImageToDCTest() {
	AccountConfig config = new AccountConfig();
	config.setAuthenticationMethod(AuthenticationMethod.KEYSTONE_V3);
	config.setUsername("user");
	config.setPassword("password");
	config.setAuthUrl("https://osphdc.server01.com:13000/v3/auth/tokens");
	config.setTenantName("default");

	Account account = new AccountFactory(config).createAccount();

	Container container = account.getContainer("images");
	if(!container.exists()) {
		container.create();
		container.makePublic();
	}

	StoredObject object = container.getObject("jenkins-logo.png");
	object.uploadObject(new File("D:\\jenkins-logo.png"));
	System.out.println("Public URL: "+object.getPublicURL());
}

Later on, we can check the result on our OpenStack object store page.

Google+

[Keycloak] How to Solve “/openid-connect/userinfo” which Gives JWT Response instead of A Simple JSON

Previously expecting a json response from “/openid-connect/userinfo” API , such as below screenshot,

But suddenly someone change configuration somewhere which makes previous response changed into,

Took quite sometime for me to findout which part of Kecloak configuration which makes this happens. Finally i found the proper configuration, it is located on “Fine Grain OpenID Connect Configuration”. Previously it is “RS512”, changing into “unsigned” solve my problem.

Google+