Extremely Serious

Category: Java (Page 3 of 3)

Listing the Entries of a Java Keystore

Use the following command to list the entries of a Java keystore:

The keytool is normally found in $JAVA_HOME/jre/bin (i.e. the $JAVA_HOME variable is where you’ve installed JDK).

keytool -list -v -keystore <KEYSTORE_FILE> -storepass <KEYSTORE_PASSWORD>

Include the -a <ALIAS> parameter to just display a single certificate.

Token Description
KEYSTORE_FILE The target key store file (e.g. cacerts found in $JAVA_HOME/jre/lib/security)
KEYSTORE_PASSWORD The password for accessing the keystore (i.e. the default is changeit)

Importing a Certificate to Java Keystore

Use the following command in importing a certificate to Java keystore:

The keytool is normally found in $JAVA_HOME/jre/bin (i.e. the $JAVA_HOME variable is where you've installed JDK).

keytool -importcert -alias <ALIAS> -v -keystore <KEYSTORE_FILE> -file <INPUT_FILE> -storepass <KEYSTORE_PASSWORD>
Token Description
ALIAS Alias name of the entry to process
KEYSTORE_FILE The target key store file (e.g. cacerts found in $JAVA_HOME/jre/lib/security)
INPUT_FILE Input file name (i.e. certificate file like cer, crt or pem)
KEYSTORE_PASSWORD The password for accessing the keystore (i.e. the default is changeit)

 

Basic log4j.properties for console and file output

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Console output
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n

# File output
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./logs/logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%t - %m%n

Install Jabba in Powershell behind proxy

Jabba is a java version manager (see https://github.com/shyiko/jabbathat simplifies the installation and switching of/to different JDK.

If your powershell is behind the firewall follow the procedure here.

Install Jabba using the following cmdlet:

Note: If you encountered something like jabba.ps1 cannot be loaded because running scripts is disabled on this system. For
more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170 see running unsigned script.

Invoke-Expression (
        wget https://github.com/shyiko/jabba/raw/master/install.ps1 -UseBasicParsing
    ).Content

Calling Web Service without Generating Stubs (JAX-WS API)

Introduction

Normally calling a SOAP WebAPI requires the developer to access WSDL then based on it generates the stubs linked to a particular server. After generating it, we need to compile those to make them ready to be used. The problem with that one is. Do we use the same server for development, testing and production? If yes, we need to regenerate the stubs for testing and if we are satisfied with the testing. We need to regenerate the stubs for production. Wait? The one tested is not really the one going to production. The simplest way to do is not to generate the stubs but understand how the soap message was structured. If we know the structure of the SOAP message (i.e. SOAP UI can be used here), then we can create it by hand. The following is the sample of assembling a SOAP message for invoking the evaluate method from the StudioService endpoint of PolicyCenter 7 (i.e. One of the Centers of Guidewire) using Java.

Code 1. Using JAXWS API with Java

import java.util.Iterator;

public class GWStudioService {

    public static void main(String[] args) throws SOAPException, IOException {
        String endpointUrl = "http://localhost:8180/pc/ws/gw/internal/webservice/studio/StudioService";
        String targetNamespace = "http://guidewire.com/pl/ws/gw/internal/webservice/studio/StudioService";

        QName serviceName = new QName(targetNamespace,"evaluateGosu");
        QName portName = new QName(targetNamespace,"StudioServiceSoap12Port");

        /** Create a service and add at least one port to it. **/
        Service service = Service.create(serviceName);
        service.addPort(portName, SOAPBinding.SOAP12HTTP_BINDING, endpointUrl);

        /** Create a Dispatch instance from a service.**/
        Dispatch dispatch = service.createDispatch(portName,
                SOAPMessage.class, Service.Mode.MESSAGE);

        /** Create SOAPMessage request. **/
        // compose a request message
        MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);

        // Create a message.  This example works with the SOAPPART.
        SOAPMessage request = mf.createMessage();
        SOAPPart part = request.getSOAPPart();

        // Obtain the SOAPEnvelope and header and body elements.
        SOAPEnvelope env = part.getEnvelope();
        SOAPHeader header = env.getHeader();
        SOAPBody body = env.getBody();

        String authNS = "http://guidewire.com/ws/soapheaders";

        SOAPElement auth = header.addChildElement("authentication", "auth", authNS);
        SOAPElement username = auth.addChildElement("username", "auth");
        username.addTextNode("su");
        SOAPElement password = auth.addChildElement("password", "auth");
        password.addTextNode("gw");

        // Construct the message payload.
        SOAPElement operation = body.addChildElement("evaluateGosu", "stud",
                targetNamespace);
        SOAPElement value = operation.addChildElement("code", "stud");
        value.addTextNode("7072696e74282248656c6c6f20776f726c642229");
        request.saveChanges();

        System.out.println("Request\n");
        request.writeTo(System.out);

        SOAPMessage response = dispatch.invoke(request);

        System.out.println("\n\nResponse\n");
        response.writeTo(System.out);

        SOAPPart inSOAPPart = response.getSOAPPart();
        SOAPEnvelope inEnv = inSOAPPart.getEnvelope();
        SOAPBody inBody =  inEnv.getBody();

        Iterator inGosuResp = inBody.getChildElements(new QName(targetNamespace, "evaluateGosuResponse"));
        Iterator inReturn = inGosuResp.next().getChildElements(new QName(targetNamespace, "return"));
        Iterator inEntries= inReturn.next().getChildElements(new QName(targetNamespace, "Entry"));

        while (inEntries.hasNext()) {
            SOAPElement inEntry = inEntries.next();

            System.out.println(">");
            System.out.println(inEntry.getElementName().getLocalName());
            System.out.println(inEntry.getTextContent());
        }
    }
}

Output 1: Sample output of running code 1

<soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Header/><soap12:Body><evaluateGosuResponse xmlns="http://guidewire.com/pl/ws/gw/internal/webservice/studio/StudioService">

<return>

<Entry>48656C6C6F20776F726C640D0A</Entry>

<Entry/>

</return>

</evaluateGosuResponse></soap12:Body></soap12:Envelope>

To understand the output of the entry we can use the following online Hex to String converter.

https://codebeautify.org/hex-string-converter

Reference:

https://axis.apache.org/axis2/java/core/docs/jaxws-guide.html#DispatchClient

Newer posts »