Rounding BigDecimal into scale 2 in WebService

BigDecimalConverter class:
final class BigDecimalConverter {
private final static Logger logger = LoggerFactory.getLogger(BigDecimalConverter.class);

private static final int SCALE = 2;

static String convertToString(BigDecimal bigDecimal) {
if (bigDecimal == null) {
return null;
}

logger.info("Rounding bigDecimal {} into scale {}", bigDecimal, SCALE);
return bigDecimal.setScale(SCALE, RoundingMode.HALF_UP).toString();
}
}
BigDecimalXmlAdapter class:
public class BigDecimalXmlAdapter extends XmlAdapter {
private final static Logger logger = LoggerFactory.getLogger(BigDecimalXmlAdapter.class);

@Override
public BigDecimal unmarshal(String s) {
logger.info("Unmarshal String: {}", s);

try {
return new BigDecimal(s);
} catch (NumberFormatException e) {
return null;
}
}

@Override
public String marshal(BigDecimal bigDecimal) {
logger.info("Marshal BigDecimal {}", bigDecimal);

return BigDecimalConverter.convertToString(bigDecimal);
}
}
package-info.java file:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://app.host.pl/api/types", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters({
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value = pl.marceen.soap.control.BigDecimalXmlAdapter.class, type = java.math.BigDecimal.class)
})
package pl.host.app.api.types;

Testing for SimpleDateFormat thread safety

https://www.dontpanicblog.co.uk/2013/12/08/testing-for-simpledateformat-thread-safety/

How to examine PostgreSQL server's SSL certificate?

Save script info file postgres_get_server_cert.py
#!/usr/bin/env python

import argparse
import socket
import ssl
import struct
import subprocess
import sys
import urlparse


def main():
args = get_args()
target = get_target_address_from_args(args)
sock = socket.create_connection(target)
try:
certificate_as_pem = get_certificate_from_socket(sock)
print certificate_as_pem
except Exception as exc:
sys.stderr.write('Something failed while fetching certificate: %s' %
exc.message)
sys.exit(1)
finally:
sock.close()


def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('database', help='Either an IP address, hostname or'
' URL with host and port')
return parser.parse_args()


def get_target_address_from_args(args):
specified_target = args.database
if '//' not in specified_target:
specified_target = '//' + specified_target
parsed = urlparse.urlparse(specified_target)
return (parsed.hostname, parsed.port or 5432)


def get_certificate_from_socket(sock):
request_ssl(sock)
ssl_context = get_ssl_context()
sock = ssl_context.wrap_socket(sock)
sock.do_handshake()
certificate_as_der = sock.getpeercert(binary_form=True)
certificate_as_pem = encode_der_as_pem(certificate_as_der)
return certificate_as_pem


def request_ssl(sock):
# 1234.5679 is the magic protocol version used to request TLS, defined
# in pgcomm.h)
version_ssl = postgres_protocol_version_to_binary(1234, 5679)

packet = '%(length)s%(version)s' % {
'length': struct.pack('!I', 8),
'version': version_ssl,
}
sock.sendall(packet)
data = read_n_bytes_from_socket(sock, 1)
if data != 'S':
raise Exception('Backend does not support TLS')


def get_ssl_context():
# Return the strongest SSL context available locally
for proto in ('PROTOCOL_TLSv1_2', 'PROTOCOL_TLSv1', 'PROTOCOL_SSLv23'):
protocol = getattr(ssl, proto, None)
if protocol:
break
return ssl.SSLContext(protocol)


def encode_der_as_pem(cert):
# Forking out to openssl to not have to add any dependencies to script,
# preferably you'd do this with pycrypto or other ssl libraries.
cmd = ['openssl', 'x509', '-inform', 'DER']
pipe = subprocess.PIPE
process = subprocess.Popen(cmd, stdin=pipe, stdout=pipe, stderr=pipe)
stdout, stderr = process.communicate(cert)
if stderr:
raise Exception('openssl errored when converting cert to PEM: %s' %
stderr)
return stdout.strip()


def read_n_bytes_from_socket(sock, n):
buf = bytearray(n)
view = memoryview(buf)
while n:
nbytes = sock.recv_into(view, n)
view = view[nbytes:] # slicing views is cheap
n -= nbytes
return str(buf)


def postgres_protocol_version_to_binary(major, minor):
return struct.pack('!I', major << 16 | minor)


if __name__ == '__main__':
main()
Save certificate into file cert.txt
postgres_get_server_cert.py example.com:5432 | openssl x509 > cert.txt
Check certificate dates:
postgres_get_server_cert.py example.com:5432 | openssl x509 -noout -dates
Check full certificate:
postgres_get_server_cert.py example.com:5432 | openssl x509 -noout -text
source:

How to make mock to void methods with Mockito

Call Real Method

Mock not void method:
Mockito.when(jsonResponseValidator.validate(any(DefaultResponse.class))).thenCallRealMethod()
Mock void method:
Mockito.doCallRealMethod().when(jsonResponseValidator).validate(any(DefaultResponse.class));
You can use any of the doReturn(), doThrow(), doAnswer(),doNothing(), doCallRealMethod() family of methods.

Do Throw

Mock void method with doThrow():
...

@Mock
privare TransferSender transferSender;

...
Transfer transfer = new Transfer();
Mockito.doThrow(new MyException("kabooom!").when(transferSender).send(transfer);
source: Mockito API

JavaEE - wyświetlenie całego URI za pomocą UriInfo

UriInfo (za pomocą którego jest możliwe wyciągnięcie całego URI) można wstrzyknąć za pomocą adnotacji @Context jako pole klasy:
package pl.marceen.test.boundary;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

@Stateless
@Path("test")
public class TestRestReceiver {
private final static Logger logger = LoggerFactory.getLogger(TestRestReceiver.class);

@Context
private UriInfo uriInfo;

@GET
@Path("logStatus")
public String logStatus() {
logger.info("Path: {}", uriInfo.getAbsolutePath());

return "OK";
}
}
Można wstrzyknąć UriInfo również jako atrybut metody:
package pl.marceen.test.boundary;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

@Stateless
@Path("test")
public class TestRestReceiver {
private final static Logger logger = LoggerFactory.getLogger(TestRestReceiver.class);

@GET
@Path("logStatus")
public String logStatus(@Context UriInfo uriInfo) {
logger.info("Path: {}", uriInfo.getAbsolutePath());

return "OK";
}
}