IT Cuties and Code Babes – Why Oh Why?

An acquaintance of mine posted a link to the IT Cuties site. The description is “Programming tutorials hosted by cute girls”. Not women, but girls.

This led me to discover the even more explicit Code Babes where you can learn to code while “checkin’ out babes”. You can go from virgin to advanced in the subject matter. Yeah, subtle.

Source - http://knowyourmeme.com/memes/facepalm

Source – http://knowyourmeme.com/memes/facepalm

Most programmers are male and we’ve managed to entrench sexist behaviour into our culture. The above sites are clear examples of that sexist drivel. If you care about our industry at all, please don’t support the behaviour and call it out when you see it.

Handy tip – If you link to a site and don’t want that to help it’s page rankings, be sure to include nofollow in your links.

Posted in IT | Tagged | 4 Comments

HMAC SHA512 in Java

When doing some research on authentication tokens I came across the post Don’t Hash Secrets and learn’t of a few new things, one of which was HMAC: Hash-based message authentication code.

I use Google Guava for some utility functions, but it doesn’t have a built in HMAC function. The code below still uses Guava for BaseEncoding.

private static final String DEFAULT_ENCODING = "UTF-8";
private static final String HMAC_SHA512 = "HmacSHA512";
 
public static byte[] hmacSha512(String value, String key){
    try {
        SecretKeySpec keySpec = new SecretKeySpec(
                key.getBytes(DEFAULT_ENCODING),
                HMAC_SHA512);
 
        Mac mac = Mac.getInstance(HMAC_SHA512);
        mac.init(keySpec);
        return mac.doFinal(value.getBytes(DEFAULT_ENCODING));
 
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
 
public static String asHex(byte[] bytes){
    return BaseEncoding.base16().lowerCase().encode(bytes);
}

I used the HMAC Generator / Tester Tool to generate test data.

Posted in Programming | Tagged , , , | Leave a comment

Submit HTML form using AngularJS

The payment gateway I’m integrating with requires a post from a form to navigate to their payment page. Some of the information I need to post is dependent on the results from a call to my back end server.

My first attempt was to invoke my controller from a regular JavaScript function called in onsubmit. However, as all $http calls in AngularJS are asynchronous there was no way to wait for the result of my back end server call before either allowing or blocking submission of the form.

The approach that worked was to create the form without an action or method. When the ‘submit’ button is clicked, the controller function (createPaymentAndNavigate) is called. This does the call to the back end, in the success callback sets the action and method on the form, and then submits the form.

HTML

<form id="bookingForm" name="bookingForm" novalidate >
...
<button class="btn btn-success btn-lg pull-right" ng-click="createPaymentAndNavigate(bookingForm.$valid)">Payment</button>
...
</form>

Success callback in AngularJS controller

var bookingForm = document.getElementById('bookingForm')
bookingForm.action = "https://www.example.com/xyz/process.trans";
bookingForm.method = "POST";
bookingForm.submit();
Posted in Programming | Tagged , | Leave a comment

Xamarin View Fails To Bind To Model

MvxBind:Warning: 9.81 Unable to bind: source property source not found Property:SomeObject on null-object

One of the causes of the above error is mixing up the order of base.OnCreate and SetContentView calls in the Views OnCreate method.

Below is an example of the correct order.

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.MyLayout);
}
Posted in Programming | Tagged , | Leave a comment

5 Minute NLog Wrapper: Limiting The Number Of Emails Sent

If you’re using an Email target to send notifications when an error occurs, it may be useful to limit the frequency of those emails. One way of achieving this is to wrap your target that sends email in a Wrapper target. This custom target will intercept calls to the Email target and, based on the time since the last email was sent, either pass on the log event to the wrapped target or swallow it.

[Target("FrequencyWrapper", IsWrapper = true)]
public class NLogFrequencyWrapper : WrapperTargetBase
{
    private DateTime _lastLogEventOccurredAt = DateTime.MinValue;
 
    protected override void Write(AsyncLogEventInfo logEvent)
    {
        if ((DateTime.Now - _lastLogEventOccurredAt).TotalMinutes >= MinLogIntervalInMinutes)
        {
            _lastLogEventOccurredAt = DateTime.Now;
            this.WrappedTarget.WriteAsyncLogEvent(logEvent);
        }
        else
        {
            logEvent.Continuation(null);
        }
    }
 
    [DefaultValue(30)]
    public int MinLogIntervalInMinutes { get; set; }
}
<extensions>
  <add assembly="MyAssembly"/>
</extensions>
 
<target name="limitedEmail" type="FrequencyWrapper" minLogIntervalInMinutes="40">
  <target name="email" xsi:type="Mail"
      ...
  />
</target>
Posted in Programming | Tagged , | 3 Comments

JEE7 Launch with Arun Gupta

The Jozi JUG is hosting Arun Gupta on 26 August for the launch of JEE7. You can RSVP on Meetup. See you there!

Arun Gupta will be flying in (not to Waterkloof) to give a presentation to the Jozi JUG on all the new goodness in JEE7. It’s not often we get international speakers, let alone of this caliber, so this one is not to be missed!

About the speaker:
Arun Gupta is a Java EE & GlassFish Evangelist working at Oracle. Arun has over 14 years of experience in software industry working in various distributed computing technologies, Java(TM) platform, and several web-related technologies. In his current role, he works very closely to create and foster the community around Java EE & GlassFish and related technologies.

To find out more about Arun, head over to https://blogs.oracle.com/arungupta/

Duke says... thumbs up to Arun!

Duke says... thumbs up to Arun!

Posted in Uncategorized | Leave a comment

Unit Testing WebMethods Services With JUnit

Unit tests are ubiquitous (or at least should be) amongst software projects. Some environments are easier to unit test than others, and WebMethods is somewhere in the middle. As the services run on a server and the tests in your IDE, we need to connect to the server to run tests. Strictly speaking it’s integration testing, but as the server is really an extension of your IDE I’m going to call it unit testing.

Step 1: Setup your dependencies

I’m using Maven. Set up instructions for the client jar dependencies can be found here.

Step 2: Create your tests

I’ve included setup and teardown methods that are invoked once to create and destroy the expensive Context variable through which all services are called.

package example;
 
import com.wm.app.b2b.client.Context;
import com.wm.app.b2b.client.ServiceException;
import com.wm.data.IData;
import com.wm.data.IDataCursor;
import com.wm.data.IDataFactory;
import com.wm.data.IDataUtil;
import org.junit.AfterClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
 
/**
 *
 * @author Michael.Williams
 *
 */
public class TestWMString {
    private static final String INPUT_LABEL = "inString";//Input variable name matching the service
    private static final String OUTPUT_LABEL = "value";//Output variable name matching the service
 
    //The Context is the variable we will connect to the server with and invoke services through.
    private static Context context = new Context();
 
    public TestWMString() {
    }
 
    /**
     * The Context is expensive to create and so is done through a method annotated @BeforClass.
     * 
     * @throws ServiceException 
     */
    @BeforeClass
    public static void setup() throws ServiceException {
        String server = "localhost:5555";
        String username = "Administrator";
        String password = "manage";
 
        context.connect(server, username, password);
    }
 
    /**
     * We must remember to disconnect the Context from the server once we have completed our unit tests.
     * 
     * @throws ServiceException 
     */
    @AfterClass
    public static void teardown() throws ServiceException {
        context.disconnect();
    }
 
    /**
     * This method handles the details of calling the service.
     * 
     * @param inputString
     * @return
     * @throws ServiceException 
     */
    private int invokeStringLengthService(String inputString) throws ServiceException {
        IData input = IDataFactory.create();//IData objects are data structures used to store input and output variables for service invocation.
        IDataCursor cursor = input.getCursor();//The cursor is used to traverse and manipulate an IData object.
        IDataUtil.put(cursor, INPUT_LABEL, inputString);//IDataUtil makes it easy to manipulate add and find variables in an IData object. Here we're adding an input variable called 'inString'
 
        IData output = context.invoke("pub.string", "length", input);//This invokes the service on the server. We pass in the package and name of the service as well as our input variables.
 
        IDataCursor outputCursor = output.getCursor();
        return IDataUtil.getInt(outputCursor, OUTPUT_LABEL, -1);//We retrieve the output variable from the IData object as an int. Various convenience methods are available to get other types of objects.
    }
 
    /**
     * Our unit test that double checks Software AG's math.
     * 
     * @throws ServiceException 
     */
    @Test
    public void testStringLength() throws ServiceException {
        int length = invokeStringLengthService("12345");
        assertEquals(5, length);
    }
}
Posted in Programming, Software AG | Tagged , , , | 2 Comments

WebMethods Client – java.lang.NoClassDefFoundError: iaik/x509/X509Certificate

If you’re calling a WebMethods service from a client and you get the error below, you need to add the enttoolkit jar to your classpath. It can be found under the Software AG installation directory on the server: \common\lib\ext\enttoolkit.jar

java.lang.NoClassDefFoundError: iaik/x509/X509Certificate
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	at com.wm.net.HttpURLConnection.getHttpURLConnection(HttpURLConnection.java:105)
	at com.wm.app.b2b.client.BaseContext.getURLConnection(BaseContext.java:456)
	at com.wm.app.b2b.client.Context.invoke(Context.java:950)
	at com.wm.app.b2b.client.Context.invoke(Context.java:900)
	at com.wm.app.b2b.client.Context.connect(Context.java:541)
	at com.wm.app.b2b.client.Context.connect(Context.java:495)
Posted in Programming, Software AG | Tagged , | 1 Comment

WebMethods Client – NoClassDefFoundError: javax/mail/internet/ParseException

If you’re calling a WebMethods service from a client and you get the error below, you need to add the java mail jar to your classpath.

java.lang.NoClassDefFoundError: javax/mail/internet/ParseException
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	at com.wm.util.Base64.encode(Base64.java:77)
	at com.wm.app.b2b.client.BaseContext.setAuthentication(BaseContext.java:598)
	at com.wm.app.b2b.client.Context.connect(Context.java:492)
Posted in Programming, Software AG | Tagged , | Leave a comment

WebMethods Client Maven Dependencies For 8.2.2.0

If you’re invoking WebMethods services from a Java client, you need 3 jars, two of which are specific to Software AG.

Step 1: Add dependencies to pom

<!-- Software AG specific dependencies-->
<dependency>
    <groupId>com.softwareag</groupId>
    <artifactId>wm-isclient</artifactId>
    <version>8.2.2.0</version>
</dependency>
<dependency>
    <groupId>com.softwareag</groupId>
    <artifactId>enttoolkit</artifactId>
    <version>8.2.2.0</version>
</dependency>
<!-- Java Mail dependency-->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

Step 2: Finding the jars

The jars can be found under the Software AG installation directory on the server.
\common\lib\wm-isclient.jar
\common\lib\ext\enttoolkit.jar

Step 3: Add jars manually to local repository

mvn install:install-file -DgroupId=com.softwareag -DartifactId=wm-isclient -Dversion=8.2.2.0 -Dpackaging=jar -Dfile=wm-isclient.jar
mvn install:install-file -DgroupId=com.softwareag -DartifactId=enttoolkit -Dversion=8.2.2.0 -Dpackaging=jar -Dfile=enttoolkit.jar
Posted in Programming | Tagged , , | 1 Comment