View what’s inside the Keystore

I have a file with java keystore type that I would like to inspect. Can the contents of the keystore of java be viewed if I don’t have it password? I want to verify an SSL certificate is inside of it. Maybe also check the details are correct, expiration date and so on. What tool can be used to open the keystore?

ANSWER

The normal notion that because the Java Keystore (JKS for short) asks for a password, then I cannot see what is in it is not entirely correct. The password for the JKS does not prevent that. Instead the purpose of the password is to protect the integrity of the JKS. The intention is that without the password, I should not be able to modify the contents, such as adding certificates or deleting some.

With the proper tool I can still read what is inside of a JKS. Normally we just use the keytool. This command line tool usually comes with the Java installation in your system. Try to find it in the installation directory of Java. It resides together with other Java binaries in the ../bin/ directory.

Without password warnings will be displayed, like this one:

*****************  WARNING WARNING WARNING  *****************
* The integrity of the information stored in your keystore  *
* has NOT been verified!  In order to verify its integrity, *
* you must provide your keystore password.                  *
*****************  WARNING WARNING WARNING  *****************

How to print what is inside the JKS:

(1) View all as a list

:~$ keytool -list -keystore /path/to/keystore/file

(2) View all as a list with details

:~$ keytool -list -v -keystore /path/to/keystore/file

(3) View a specific entry only using alias with details (The alias is the text that comes out in command #1 before the date, without the comma)

:~$ keytool -list -alias "the alias text" -v -keystore /path/to/keystore/file

How to convert For loop using Stream in Java

Creating a Map of Applicant object, where I filter out on the applicant’s age. I only need the first name and last name of the applicant. Using the application ID as the key. The ID is generated integer and unique. Also, testing for null, don’t want that in there. I am using a traditional for loop where I am most comfortable at. But I want to use the Java 8 Stream instead. How is it done?

My code for this is below:

        Applicant a1 = new Applicant();
        a1.setId(1001);
        a1.setFirstName("Joseph");
        a1.setLastName("Dey");
        a1.setAge(25);

        Applicant a2 = new Applicant();
        a2.setId(2001);
        a2.setFirstName("Maxine");
        a2.setLastName("Summers");
        a2.setAge(21);

        Applicant a3 = new Applicant();
        a3.setId(3001);
        a3.setFirstName("Jimmy");
        a3.setLastName("Cox");
        a3.setAge(17);

        Applicant a4 = null;

        List<Applicant> list = new ArrayList<>();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        list.add(a4);

        Map<Integer, String> map = new HashMap<>();
        for (Applicant a : list) {
            if (a != null && a.getAge() > 18) {
                map.put(a.getId(), a.getFirstName() + " " + a.getLastName());
            }
        }

ANSWER

By Statement Lambda in Collectors.toMap – right-hand side is a block. This can become longer to write but sometimes when you have to do more transformations, then it can’t be avoided.

1       Map<Integer, String> map = list.stream()
2                .filter(applicant -> applicant != null)
3                .filter(applicant -> applicant.getAge() >= 18)
4                .collect(Collectors.toMap(applicant -> applicant.getId(), applicant -> {
5                    return applicant.getFirstName() + " " + applicant.getLastName();
6                }));

By Expression Lambda in Collectors.toMap – right-hand side is an expression.

1       Map<Integer, String> map = list.stream()
2                .filter(applicant -> applicant != null)
3                .filter(applicant -> applicant.getAge() >= 18)
4                .collect(Collectors.toMap(applicant -> applicant.getId(), applicant -> applicant.getFirstName() + " " + applicant.getLastName()));

In both cases above, line #2 the Lambda can be replaced with method reference too. It will look like:

.filter(Objects::nonNull)

And on line #4, the same can be done for the Lamba replacing it with a method reference. It will look like:

.collect(Collectors.toMap(Applicant::getId,  // rest of code ommitted

Convert a Map to POJO in Java

How do I convert a Map object to my other plain old Java object (POJO) without going into loops and having to write a class using Java reflection, or some other?

ANSWER

Well, yes, reflection is one but you didn’t want to do that yourself. For some good reason, I bet. It’s a good exercise if you have all the time in the world. But when faced with deadlines and having to write Unit tests for a whole class you wrote, there must be an easier way.

There is more than one way, but what I normally use is the Jackson ObjectMapper. Yes, the same one from the com.fasterxml.jackson library.

Anyway, with ObjectMapper it is pretty straightforward to do so. If I have a Person class like this:

    public class Person {
        private String firstName;
        private String lastName;
    }

My Map object will look like this:

        Map<String, Object> map = new HashMap<>();
        map.put("firstName", "Johnny");
        map.put("lastName", "Foo");

Then with ObjectMapper one can simply do this:

        ObjectMapper mapper = new ObjectMapper();
        Person person = mapper.convertValue(map, Person.class);

Alternatively, the keys in the Map may not align with the fields in the Person class. Well, I usually encounter this when working with JSON objects with lots of crazy looking field names. Something like this – NZT_Mor_First_Name__c – which I clearly don’t want my class field name to be like.

Well, we can use @JsonProperty annotation which is part of Jackson by the way and assign that our class field. The Person class will now look like:

    public class Person {

        @JsonProperty("NZT_Mor_First_Name__c")
        private String firstName;

        @JsonProperty("NZT_Mor_Last_Name__c")
        private String lastName;
    }

Again the Map will hold these values:

        Map<String, Object> map = new HashMap<>();
        map.put("NZT_Mor_First_Name__c", "Jose");
        map.put("NZT_Mor_Last_Name__c", "Yamut");

Now it will map out those weird looking key names to its corresponding class fields.

One thing to note is you might need to set ObjectMapper features such as ignoring unknown properties and make it case insensitive. Allow it a bit more room to wiggle, wiggle.

        ObjectMapper mapper = new ObjectMapper()
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                .enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES);

How to make IntelliJ check and create SerialVersionUID

When the class implements Serializable I want the serialVersionUID field to be auto-created by IntelliJ IDE. This feature comes in automatically in Eclipse. But the former does not.

How do I make IntelliJ create the field or warn me that it is not defined in the class yet?

ANSWER

It is not turned on by default in IntelliJ. Can be easily done by going to the following:

File > Settings > Editor > Inspections > Java > Serialization Issues

Then find and select – Serializable class without ‘serialVersionUID’ – by checking on the box to the right of it.

Similarly, you can search for it once in File > Settings. Keyword for search can be – ‘serialVersionUID’ – without the quotes.

The path or location of this feature on IntelliJ may vary. It should be the same on Linux and Windows for versions 2019 and 2020.

This will only throw a warning. It does not magically create the field in all those classes that implements Serializable straight away. But then IntelliJ will help you create it, like any other decent IDE out there. Go to the class name declaration, you may find that it is subtly highlighted in yellow to hint that there is a user action that can be done. Hover then click on the option – Add ‘serialVersionUID’ field – and you will get the desired result like this one:

private static final long serialVersionUID = 6265605531073886420L;

Database tables map One To Many without foreign key in @JoinColumn

Possible to not have foreign key relationship in child table to its parent in the database, while in JPA it will still be able to map out fine? How will this be done?

Can I still use @JoinColumn to define the field for reference back to the parent table?

This is a one-to-many relationship. One parent record, many child records.

I only care about writes to the database tables right now.

ANSWER

Yes, no FKs defined in the database is fine. JPA will still be able to map out there relationships as defined in your entity classes.

In fact, enabling – spring.jpa.generate-ddl – in your application properties file will create the necessary Foreign Key and/or Unique Key constraints in the database. This is vendor dependent however.

For a One To Many relationship, it is straightforward to do. Example below definition below:

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "recommendation_id")
    private List<CommentEntity> commentList;

This will map out the entity object values to the Comment table, assuming that is the name of the target table. Each one will be persisted to the child table along with the ID of the parent Recomendation under the recommendation_id column.

How to ignore unknown fields when parsing JSON using Jackson

In Java, there is Jackson 2 library that is very popularly used when reading JSON objects and mapping those values out to a POJO.

Happy path, as long a I have all the fields from the JSON defined in the POJO then okay. In a perfect world it will work 100%.

But there is no perfect world. The JSON object in other scenarios can contain unknown fields. These are not in my POJO. Application fails from reading that JSON to POJO.

Getting this error:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "status" (class com.example.dto.Evaluation), not marked as ignorable (6 known properties: "Book",

ANSWER

Use this Jackson annotation at a class level. This has to be configured per class. That should do it. Will ignore any unknown properties silently including nested objects.

@JsonIgnoreProperties(ignoreUnknown = true)

When you are using Jackson ObjectMapper directly, that can be configured to ignore unknown properties globally as well.

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

How Do I Decode An OpenShift Secret In A Mounted Volume?

Mounting an OpenShift Secret to its own volume is straightforward. There are plenty of examples on how to do it. The Web is littered with examples. Next, the most common thing any manual, guide or tutorial would say is to encode the secret in Base64.

For example I have a SSL certificate stored in a Java KeyStore file format or JKS. The recommended way is to store it in a Secret instead of ConfigMap since it is sensitive information. Of course, that goes without saying the JKS file is password-protected.

Second, it must be as a Base64 string before I save it as OpenShift Secret.

How do I get the Base64 string to be decoded in the mounted volume? This one does not seem to get many answers.

Do I need to include shell commands to decode it and write it to a folder?

Which folder should I write it to as best practice?

ANSWER

#1 The encoded JKS Secret as an environment variable

This is an option but I’m not a fan of it. The JKS file when encoded can become very long, especially when the file size is large. I don’t think environment variables were meant to be used like this – hold very long text values. That said, this is an easier implementation.

Map out the secret as an env var in OpenShift or Kubernetes. Then you can use that env var to echo the value and write it out to a file.

echo -n $SECRET_JKS_VAR | base64 --decode > /file/path/to/decoded-secret.jks

#2 Mount for read, mount for write

You have the secret mounted to a file path. That is for reading. Now you need to decode, which means writing to a file so your application can read it back unencoded.

First, define a mount point as and empty directory. It must be writable. Then make it memory only.

Next, read the JKS file from its mount point, write it out to the empty dir mount point.

cat /mount/file/path/for-reading/encoded-secret.jks | base 64 --decode > /mount/file/path/for-writing/decoded-secret.jks

I like this approach better. It makes more sense rather than mapping a very long base64-encoded text value as an environment variable. The caveat is here is slightly more configurations to be made. Also if I’m not mistaken the in-memory volume count against your app memory quota. Should be negligible unless you write thousands of files into it.

#3 Lastly, write where you can

Underneath that container is a file system. Where you have permission to write, then do so there. If it’s Linux, then this decision is pretty much arbitrary IMHO. Put it in /tmp or /home or /mnt. Security wise, others might have access to your app’s pod, that means they can get to the Secret as well. But that is another topic.

How do I use an event to return true in an if condition in Java

I am new to Java and I am making an app where I need to have multiple containers do the same kind of thing. What I plan to do is use the event objects to find out which one was selected and then call the same function. I don’t have any code at the moment.
How would I use the event object to do so? Or is there a better way?

Go to Source
Author: Dan Morrissey

Deployment of Docker containers on ECS(Tcp Server Java)

The given problem:

  • Created a Java Tcp Server with Eclipse vertx(Toolkit for building
    reactive,event driven)applications on JVM. The applicatino has two
    Tcp Servers which are running on port numbers:
    server1(port:5050);server2(5051)

  • I created a Docker image and was running the application inside a
    container and all works fine locally.

  • I want to deploy the docker container on ECS(Amazon Container
    Service) and to use Network Load balancer,the question is how can i
    assign different ports to different containers if in the Java code i
    was setting up explicity

//tcp server 1 listens on port 5050

server..listen(5050)
  • and how does ECS manages what ports to assign to what container?

    The tcp client is always targeting one ip adress and one port number
    it cannot be changed.

    Thanks!

Go to Source
Author: user581026

How to get Json [object Object] type in Jsp?

actually I got that Json from Java to jsp.

$.ajax({
url:”MovieList.me”,
method:”get”,
dataType :”json”,
success:function(data){
$.each(data.Data,function(idx,item){

                    $.each(item.Result,function(idx,item2){
                        $.each(item2,function(idx,item3){
                         $('table').append("<tr><th>"+item3+"</th></tr>")
                        });
                      });

this is Jsp Jquery Code.

JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject) jsonParser.parse(json);

this is java code and return jsonObject

Thank you for read

Go to Source
Author: 이태윤

Pattern name for “Platform independant class with Hooks + sub class implementing platform specific”?

I am developing an android app to process jpg exif meta data in a workflow.

The Workflow class is implemented platform independant (i.e. code runs on android and on j2se).

The Workflow contains and calls protected virtual methods that do nothing for android specific .functions (i.e save changes to android media database)

package de.k3b.media;

// platform independant
public class Workflow {
    public void processPhoto(...) {
        ...
        saveChangesToMediaDatabase(....);
        ...
    }
    
    protected void saveChangesToMediaDatabase(...) {    
        // to nothing in platform independant version
    }
}

I also have a android platform specific sub class that implements the platform specific code

package de.k3b.android.media;

// android platform dependant
public class AndroidWorkflow extends Workflow {
    @Override
    protected void saveChangesToMediaDatabase(...) {    
        ... do something with android media database
    }
}

I can use Workflow.processPhoto(...) in non-android j2se apps and in automated unit or integration tests
and i can use AndroidWorkflow.processPhoto(...) in my android app

My question: Is this an established pattern and is there a name for this pattern?
My current pattern name “Platform independant class with Hooks” and “Platform specific Hook implementation”.

I hope to find a better (??established??) name for this pattern.


Remarks:

One established pattern to have platformindependat code is by using a Facade pattern.

Example: I use a FileFacade with a j2se implementation based on java.io.File and AndroidFileFacade which is based on android specific DocumentFile.

Although the goal of platform independance is the same the way how this is achieved is different.

Go to Source
Author: k3b

Integrating TeX into a Java desktop application

Looking to integrate TeX equations in a TeX-agnostic fashion, suitable for either ConTeXt or LaTeX, into a Java-based desktop Markdown editor. The possibilities are numerous, but I’m not sure what approach to take.

JMathTex outputs to MathML, which must be transformed. JEuclid can transform MathML to BufferedImages (not 100% sure). Neither are JDK 14-friendly and may be too slow to render in real-time. I haven’t looked because I read that they didn’t work with Java 9 (without porting effort), much less 14. Also, the licensing isn’t LGPL, which probably won’t work with the MIT licensing.

The NTS library is reported to compile gentle.tex in about 3 seconds. The spin-off project, εχTEX improves upon NTS, but I don’t know to what extent.

Java LaTeX Report requires a TeX engine.

Yet another possibility is to try JavaTex or rework the project using web2java to transpile WEB files into Java files.

JLaTeXMath does an exceptional job at rendering, though investigation would be required to see if it can perform real-time renders, output as SVG, or otherwise determine if it can integrate with FlyingSaucer.

The HTML preview panel uses FlyingSaucer. The SVG documents are rendered using SVG Salamander. If it was possible to go from TeX to SVG, that could work quite handily, architecturally. Some documentation stated FlyingSaucer can also render MathML, but I suspect the doc is wrong. FlyingSaucer doesn’t integrate with JavaScript. For that I’d have to add a JavaScript Engine.

If a JavaScript Engine is necessary, then using KaTeX may be an option.

Still another possibility is the TeX to MathML service. This is written in Java, but the source code is not available anywhere. I’ve reached out to some people involved in the project.

For native speeds, there may be a (cross-platform?) C-based TeX engine that can generate MathML or SVG from a TeX input. If so, it may be possible to integrate with the JNI.

Here’s a demo video of the app, to give you an idea of what is needed:

The goal is to produce real-time rendering of math, ideally in pure Java (no JavaScript).

What approach would you take and what do you see as its benefits and drawbacks?

Go to Source
Author: Dave Jarvis

Applicability of Java skills to Groovy development

Hi all: if you have meaningful Java experience, was it enough to develop basic Groovy code as well?

Leverice opened up its platform API and wondering how much overlap there is between Java and Groovy skills. If you’ve made the transition, how much time/work did it require to get up to speed with Groovy?

Go to Source
Author: deevee

Port Forwarding – Server responds to remote Java request, responds via local browser, can’t be reached in remote browser

I am hosting an Apache Tomcat instance on a server which is used to display a web page.
It needs to be remotely accessible via a browser.
The web page runs normally via localhost and I have setup port forwarding to make it remotely accessible.
I have tested the port forwarding with Java and successfully connected to the sockets.
Despite being able to connect to the socket, when I attempt to access it via a remote browser is returns “site can’t be reached”.

Locally
Local Browser – Web page runs as expected on localhost:8080

Remotely
Socket test – pass
Remote Java Request – Responds with full html of web page
Remote Browser – Site can’t be reached

Does anyone know why only the remote browser portion would fail, while remote requests sent through Java would work?

Go to Source
Author: jorbo

Getting exception when trying to suppress a constructor with PowerMockito

Trying to mock an instance creation:

//Class
final InformationSection infoSec = new InformationSection("123");

with

//Test
PowerMockito.suppress(MemberMatcher.constructorsDeclaredIn(InformationSection.class));
InformationSection infoSec = mock(InformationSection.class);
when(InformationSection.class).withArguments(Mockito.anyString()).thenReturn(infoSec);

The constructor of InformationSection which I am trying to suppress is internally doing
Integer.parseInt("123") to call another constructor of the same class with Integer argument and that is throwing NumberFormatException:

java.lang.NumberFormatException: null
at java.lang.parseInt(Integer.java:615)
InformationSection.<init>(InformationSection.java:40)

So my confusion is if I am suppressing the constructor, why is it still doing that parsing?
I am referring to:
What does <init> signify in a Java exception?
Is this happening before suppressing the constructor?

Go to Source
Author: Ssk92