Generic with check and catch

Suppose you have a @RestController with several calls to a repository (e.g. different attribute searches) and return the results in a list.

It might be useful to create a generic method in your controller so you don’t have that much duplicate code. In the method you can check for content, no content or some error.

Code can look like this:

protected <T> ResponseEntity<List<T>> getListEntriesWithCatch(String argument, Function<String, List<T>> retrievalFunction) {
    try {
        List<T> items = new ArrayList<>(retrievalFunction.apply(argument));
        return items.isEmpty()
                ? new ResponseEntity<>(HttpStatus.NO_CONTENT)
                : new ResponseEntity<>(items, HttpStatus.OK);
    } catch (Exception e) {
        logger.warn("Fetching list entries failes", e);
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

Now you can call it with the repository as argument, like this:

return this.<DomainClass>getListEntriesWithCatch(name, domainClassRepository::findByAttribute);

The generic method is protected, so it can be tested more easily. And because it accepts any repository, you can create a custom function in your test instead of mocking your repo.

@Test
void testGetListEntriesWithCatch() {
MyController controller = new MyController();
Function<String, List<String>> retrievalFunction = arg -> Arrays.asList(COLUMN1, COLUMN2);
ResponseEntity<List<String>> result = controller.getListEntriesWithCatch(SCHEMANAME, retrievalFunction);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals(2, Objects.requireNonNull(result.getBody()).size());
}

Automatically reload / renew java cacerts in keystore

In our current project we have a Nexus server as artifactory repository manager. Setup is not entirely finished and one drawback is that the certificate is only valid for 3 days.

The following script downloads the certificate and uses keytool to import the certificate only when it’s a new one (using the same alias)

#!/bin/bash
true | openssl s_client -connect nexus.mycompany.com:443 2>/dev/null | openssl x509 > ~/Downloads/nexus-crt.crt
DIFF=$(diff ~/Downloads/nexus-crt.crt ~/Downloads/nexus.mycompany.com.crt)
if [ "$DIFF" ]
then
   echo "Reloading Nexus certificate"
   cp ~/Downloads/nexus-crt.crt ~/Downloads/nexus.mycompany.com.crt
   cd ~/.sdkman/candidates/java/current/bin
   ./keytool -delete -alias nexus-crt -cacerts -storepass changeit
   ./keytool -importcert -noprompt -trustcacerts -cacerts -alias "nexus-crt" -file ~/Downloads/nexus.mycompany.com.crt -storepass changeit
   cd -
fi

If you want to display the certificates enddate:

echo "cert enddate: cat ~/Downloads/nexus-crt.crt | openssl x509 -noout -dates | tail -1 | sed 's/[^ ]*=//' "

gitlab git mvn pull all clean all

Suppose you have a directory /pub/gitlab with all your git projects, but also a logs directory which can be skipped.

user@server:~$ cat > cleanall
cd /pub/gitlab
for d in */ ; do
log=$(basename $d)
[[ $log =~ ^(logs)$ ]] && continue
cd "$d"
echo -n "$d : "
mvn clean
cd ..
done

user@server:~$ cat > pullall
cd /pub/gitlab
for d in */ ; do
log=$(basename $d)
[[ $log =~ ^(logs)$ ]] && continue
cd "$d"
echo -n "$d : "
git pull
cd ..
done

Offline install sqlcmd (SQL Server command line tool) on Linux CentOS

To install sqlcmd on Linux CentOS try to get hold on the following rpm’s :

unixODBC from CentOS: http://mirror.centos.org/centos/7/os/x86_64/Packages/
file: unixODBC-2.3.1-14.el7.x86_64.rpm

msodbcsql from Microsoft: https://packages.microsoft.com/rhel/8/prod/
file: msodbcsql18-18.0.1.1-1.x86_64.rpm

mssql-tools from Microsoft: https://packages.microsoft.com/rhel/7.3/prod/
file: mssql-tools18-18.0.1.1-1.x86_64.rpm

Now run:
sudo yum localinstall unixODBC-2.3.1-14.el7.x86_64.rpm
sudo yum localinstall msodbcsql18-18.0.1.1-1.x86_64.rpm
sudo yum localinstall mssql-tools18-18.0.1.1-1.x86_64.rpm

Accept license when asked and you’re done hopefully . Add to the path (in .bashrc):
export PATH=/opt/mssql-tools18/bin:$PATH

If you’re behind a firewall and your database instance is on port nnnn

sudo iptables -A INPUT -p tcp --dport nnnn -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
sudo iptables -A INPUT -p udp --dport nnnn -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

Now try:
sqlcmd -S tcp:[hostname],[port] -U [username] -P [password]
or
sqlcmd -C -S tcp:[hostname],[port] -U [username] -P [password]
like
sqlcmd -C -S tcp:some.address.com,50001 -U john -P doe

git deploy (to) empty repository on github

$ cd ../deploydemo/
$ git init
Initialized empty Git repository in C:/Dev/Projects/deploydemo/.git/
$ git add .
warning: LF will be replaced by CRLF in .gitignore.
$ git commit -m "initial commit"
[master (root-commit) 581ef49] initial commit
 17 files changed, 897 insertions(+)
git remote add origin https://github.com/username/deploydemo.git
$ git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/username/deploydemo.git'
$ git add .
$ git commit -m "initial commit"
On branch master
nothing to commit, working tree clean
$ git push origin master
Enumerating objects: 42, done.