Author Archives: purpleblob

Interacting with SOAP headers using CXF

Sometimes you might want to interact with data being passed over SOAP within the SOAP headers, for example this is a technique used to pass security tokens or user information etc.

CXF comes with quite a few “insertion points” whereby we can insert our code into the workflow of WSDL creation, SOAP calls etc. Here we’ll just look at the specifics of intercepting the SOAP call and extracting the header (ofcourse the reverse can also be implemented, whereby we intercept an outward bound call and insert a SOAP header item, but that’s for the reader to investigate).

I’m only going to cover implementing this in code, but obviously this can also be setup via Spring configuration also.

To add an interceptor to our JaxWsServerFactoryBean, we do the following

JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
// set up the bean, address etc.

org.apache.cxf.endpoint.Server server = factory.create();
server.getEndpoint().getInInterceptors().add(new SoapInterceptor());

Now let’s look at the SoapInterceptor

public class SoapInterceptor extends AbstractSoapInterceptor {
    public static final String SECURITY_TOKEN_ELEMENT = "securityToken";

    public SoapInterceptor() {
        super(Phase.PRE_PROTOCOL);
        addBefore(WSDLGetInterceptor.class.getName());
    }
    
    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        String securityToken = getTokenFromHeader(message);
        // do something with the token, maybe save in a context
    }

    private String getTokenFromHeader(SoapMessage message) {
        String securityToken = null;
        try {
            List<Header> list = message.getHeaders();
            for(Header h : list) {
                if(h.getName().getLocalPart() == SECURITY_TOKEN_ELEMENT) {
                    Element token = (Element)h.getObject();
                    if(token != null) {
                        securityToken = token.getTextContent().toString();
                        break;
                    }
                }
            }
        } catch (RuntimeException e) {
            throw new JAXRPCException("Invalid User", e);
        } catch (Exception e) {
            throw new JAXRPCException("Security Token failure ", e);
        }
        return securityToken;
    }
}

Intercepting localhost calls with Fiddler

A while back I wrote a post on Intercepting and creating SOAP messages using Fiddler.

I’ve just had to revisit this post to debug changes in a SOAP implementation which seemed to be causing unexplained failures.

So I started up the servers (running in Java) and configured my .NET client as per the previously mentioned post, but for completeness, let’s show the App.config changes here anyway

<system.net>
   <defaultProxy>
    <proxy bypassonlocal="false" usesystemdefault="true" />
   </defaultProxy>
</system.net>  

I then ran Fiddler v4.5.x.x and then the .NET application. Fiddler showed no traffic.

Fiddler and localhost from a .NET application

Hunting around, I found several possible solutions, for intercepting IE or another browser, changing localhost to localhost. was one suggestion as well as adding the Fiddler proxy in some other way, but as per Problem: Traffic sent to http://localhost or http://127.0.0.1 is not captured, simply changing localhost in your application to the machine’s name worked and now I could intercept localhost messages.

Unable to install new software in Eclipse (behind a proxy)

I’m using Eclipse to do some Java work and I wanted to install subclipse from the marketplace/via the install new software. Now I have to go through a proxy server and this can be setup easy enough, but I found the Install New Software dialog stuck at 2% for ages or Market place just does nothing. I even tried downloading the plugin I wanted and deploy manually but it wanted to access other plugins/components, meaning again I got stuck accessing anything on the web.

So how to we fix this issue…

Note: I’m using Neon at the time of writing, therefore I can only confirm that the below works for this version of Eclipse.

Setting your proxy in Eclipse

First let’s review setting up our proxy server configuration in Eclipse.

  • Select Window | Preferences
  • Select General | Network Connections
  • Switch Active Provider to Manual
  • Edit HTTP & HTTPS to set your proxy host, port, user credentials
  • You may also need to set SOCKS up for your proxy, but this might cause an issue if your proxy doesn’t support it. Spoiler Alert: this was caused a problem where I couldn’t download from the market place etc. see below
  • You may well need to Add localhost and 127.0.0.1 to the Proxy bypass option in the Network Connections dialog
  • I had HTTP, host dynamic unchecked
  • Press OK to confirm your proxy settings

Install new software or go to the Eclipse Marketplace

Now I want to install subclipse (or install new software). Either way when running install I now hit the previously mentioned problem of not completing our download from the market place.

The problem appears to be down to (if you read setting up the proxy you’ll see the spoiler), the proxy actually doesn’t support SOCKS. So if you’ve setup a host, user credentials etc. for SOCKS, select SOCKS and click the Clear button (you cannot remove the fields using Edit).

Ofcourse if your proxy does support SOCKS, I’m afraid this probably won’t help.

Now retry accessing the Install New Software or Eclipse Marketplace and all should work.

npm and a proxy server

I was just setting up npm to try out Angular and hit the age old problem of accessing the internet through a proxy server.

You can set your proxy servers as follows

npm config set proxy http://<username>:<password>@<proxy>:<port>

and likewise the https proxy as follows

npm config set https-proxy http://<username>:<password>@<proxy>:<port>

This should create the relevant configuration file (.npmrc) in your c:\Users\{username} folder on Windows.

To confirm the location you can simply type

npm config get

Note: type “npm config” to get a list of usage commands.

You can also edit the .npmrc with a text editor, which is the simplest way to update password or the likes for your proxy.

Cannot GET / in Angular

I was playing with the quick start tutorial from Angular’s setup page on their web site.

Using the file/folder layout on their THE HERO EDITOR I removed all folders/files not listed in their diagram.

For completeness I’ll duplicate the information here

angular-tour-of-heroes
|—src
|—|—app
|—|—|—app.component.ts
|—|—|—app.module.ts
|—|—index.html
|—|—main.ts
|—|—styles.css
|—|—systemjs.config.js
|—|—tsconfig.json
|—node_modules …
|—package.json

I then ran npm start from the command prompt in the root angular-tour-of-heroes and up pops the browser with Cannot GET /, looking at the output from npm I could see index.html was not found by npm.

I also noticed npm couldn’t find a file bs-config.json, so I located that from the quick start and placed it in the root angular-tour-of-heroes folder and all worked correctly, no more 404.

Here’s why, the bs-confog.json gives the baseDir of the “website”, here’s the file context

{
  "server": {
    "baseDir": "src",
    "routes": {
      "/node_modules": "node_modules"
    }
  }
}

Ubuntu server updating

Every month (at least) I check my Ubuntu servers for updates and every month (at least) I forget how I clean old installs etc.

The following is a list of commands that I use regularly. This post is not meant to be a comprehensive description of these Linux/Ubuntu commands, more a checklist for updating my servers.

df or df /boot

When I log into my servers and see updates available the first thing I tend to do is run df and check the /boot usage/space available (obviously I could jump straight to the next command in my list, but I sort of like knowing what space I have available). I’ve tried carrying out an update in the past with next to no space available and sadly end up spending way too long trying to clear up partially installed components and/or clear disc space.

Alternatively, if you only want to view the /boot usage/space – which is really what we’re after here, just use df /boot.

sudo apt-get autoremove

apt-get is the Ubuntu based package manager (well, it’s actually originally from Debian), we’ll need to run it as the su. The options supplied will remove old packages and configurations files and all unused packages.

Why is the above useful? Well as we add updates to Ubuntu, it’s not unusual to find previous releases sat on our machine in the /boot area (I assume this is so we could roll back a release). Once you’re happy with your latest updates (or as I do, prior to the next update), run this command sudo apt-get autoremove to clear out these unused packages and free up disc space.

sudo apt-get update

The update option will retrieve a new list of packages available. This doesn’t install new versions of software but will ensure all package index files are upto date.

sudo apt-get upgrade

Now its time to upgrade we run sudo apt-get upgrade. This will look to clear out old packages if required, but doesn’t seem to do a more comprehensive removal, unlike purge and autoremove.

This command uses information from apt-get update to know about new versions of packages on the machine.

sudo apt-get dist-upgrade

If we want upgrade dependencies etc. this can be used instead of the straight upgrade.

sudo reboot now

Time to restart the server. This will obviously reboot the machine instantly (now).

uname -r

I don’t tend to use this during my “update” process, but if I want to know what version of the Linux kernel I’m running, using uname -r will tell me. This does become important if (as has happened to me) I cannot autoremove old kernels. Obviously when targeting specific kernels you need to know which one is current, or to put it another way, which one to not remove.

So for example, at the time of this post my current kernel is

4.4.0-72-generic

dpkg -l | grep linux-image

If you want to see what kernels exist on your machine, use dpkg -l | grep linux-image. This will (in conjunction with uname -r) allow us to target a purge of each kernel individually, using the following command.

sudo apt-get -y purge linux-image-?.?.?-??-generic

Obviously replace ?.?.?-?? with the version you wish to remove. This will then remove that specific package.

Scala classes/objects/traits a little more in depth

Recap on what a class, object and trait are…

A class is analogous to a C# class which is created on a per instance bases. An object is a singleton, analogous to a static class in C# and a trait can how declarations of functions (without implementations) or include implementations which is analogous to a C# abstract class.

So in summary

ScalaC#
classclass
objectstatic class
traitabstract class

Class

As previously mentioned, a class is a type which we can create an instance of, and cannot have abstract (or non-implemented) functions (see trait for abstract/interface types)

class Rectangle(width: Int, height: Int) {
  def area = width * height
}

and we can extend/derive from this using the extend keyword, giving us

class Square(side: Int) extends Rectangle(side, side) {
}

Object

As previously stated, an object can be viewed as a static class (in C#), giving us

object ShapeFactory {
  def createSquare(side: Int) = 
     new Square(side)
  def createRectangle(width: Int, height: Int) = 
     new Rectangle(width, height)
}

In use, we do not need to use the new keyword (just like C#), so we can use this factory like this

val square = ShapeFactory.createSquare(5)
assert(square.area == 25)

Trait

A trait can be viewed as being analogous to an abstract class. We can have functions with implementations or no implementation so that any class which extends this trait must implement the unimplemented functions. Here’s a trait with a single abstract function (no implementation) named area and an implementation of a function named name

trait Shape {
  def area : Int
  
  def name = "Shape"
}

We cannot create a trait without implementing the area function, but this doesn’t mean we must create a class (as such), we can create what C# might call and anonymous type, for example

val s = new Shape {
   override def area: Int = 20
}

But ofcourse, we can also extend the trait with another trait or a concrete implementation as a class, for example

class Rectangle(width: Int, height: Int) extends Shape {
  def area = width * height
}

Functions, by default, are virtual (as per Java) and so we can override the name function, for example

class Rectangle(width: Int, height: Int) extends Shape{
  def area = width * height

  override def name: String = "Rectangle"
}

If we want to stop a function being overridden we can use the final keyword (again like java). So Shape might be defined as

trait Shape {
  def area : Int

  final def name = "Shape"
}

Trait as an interface

So one thing you might notice is that in C# and Java we have the concept of an interface. Scala doesn’t support a specific interface keyword. So in this way it’s similar to C++ in that we can simply use a trait without any implementations.

For example

trait Shape {
   def area : Int
}

We can extend traits, for example

trait Triangle extends Shape {
  def isRightAngle : Boolean
}

and ofcourse we could have implemented some functions within the extended type. To implement a class from a Triangle we can just write the following

class RightAngleTriangle(adjacent: Int, opposite: Int) extends Triangle {
  override def area: Int = (adjacent * opposite) / 2
  override def isRightAngle: Boolean = true
}

How to create property like functionality in Scala

Scala has a slightly strange syntax for the equivalent of C# properties. Let’s take a look

class Person(private var _name: String) {
  // getter
  def name = _name
  // setter
  def name_=(m: Int): Unit = {
    println("Setter called")
    _name = m
  }
}

// code to call the above
val c = new Person("Scooby Doo")
c.name = "Scrappy Doo"

In the Person constructor we mark the _name as private so it cannot be altered outside of the class and notice it’s marked as a var as its used as the backing field for the name property. The getter name is pretty standard but the name_= is a little stranger.

Note: There can be no space between the _ and =

This is the syntax for a setter, you do not need to use something like c.name_= “Name” it simply translates to c.Name = “Name”.

Learning Scala – day 2

In my first post on learning Scala I mentioned using ScalaTest. I’m going to walk through the steps (using IntelliJ community edition) to get up and running with ScalaTest as it’s a great way to try things out (ofourse you could alternatively use the Scala console).

  • Create a new project
  • Select Scala and SBT
  • Give the project a name, mine’s named Demo
  • If not already selected, select the JDK you want to support as well as the SBT and Scala version, or stick with the defaults if they’re already setup correctly

Upon finishing the new project wizard, you’ll get a standard layout of folders, as follows

src/main/scala
src/test/scala

In the root of the project you’ll get a build.sbt file. This is the Scala build system. If we open it, IntelliJ has supplied the bare minimum

name := "Demo"
version := "1.0"
scalaVersion := "2.12.1"

Before we do anything more let’s go into src/main/scala, right mouse click on the folder and select New Scala class. Give it the name Calculator, here’s the code

class Calculator {
  def add(a: Int, b: Int) = {
    a + b
  }

  def subtract(a: Int, b: Int ) = {
    a - b
  }
}

Now, go back to the build.sbt file and add the following

libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.1"

When the build.sbt file was changed you’ll see it asking you to Refresh the Project, Enable auto-import or Ignore. I clicked Enable auto-import.

This will then import more Extendal Libraries (if you want to see what was imported, expand the External Libraries in the project view alt+1, if not already displayed)

Now, we could have done this in one step (but I wanted to demonstrate how the external libraries get added to), but now add the following

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"

further external libraries will get imported if you refresh (or if auto-import is enabled, after a moment). These now include scalatest.

We’re going to let IntelliJ create our initial unit test fixture. To ensure everything is in place, carry out the following steps

  • Press the Project Structure button or File | Project Structure
  • In the Dependency tab or your project (i.e. mine’s demo) check the scalatest project has has been imported or add your preferred test package here
  • Press OK to close the dialog (or if no changes were made then Cancel is ofcourse fine)

Or if you’re happy all is ready then…

  • Load Calculator.scala into the editor
  • On the line of the class either right mouse click and select Goto | Test or the hot keys ctrl+shift+t
  • You should see a Create New Test popup
  • Select this to display a dialog
  • Now select the functions you want to create tests for, then press OK.

You’ll now be placed into the newly added CalculatorTest class and it’ll look like this

import org.scalatest.FunSuite

class CalculatorTest extends FunSuite {

  test("testAdd") {
  }

  test("testSubtract") {
  }
}

From here we can write our test code and further tests.

Like other test frameworks we now have access to assert functions, so for example we can have the testAdd look like this

test("testAdd") {
   val calc = new Calculator
    
    assert(calc.add(1, 2) == 3)
  }

org.scalatest contains a whole bunch of assert functions that we can use in our tests.

ScalaTest is not the only scala based test library

References

Working with Scala Tests
ScalaTest

Learning Scala – day 1

I’ve decided to learn (at least the basics) of Scala. This post will cover the standard programming constructs and features, whilst not intended to be in depth, it will act as an entry into using Scala or a reminder for myself.

Note: I will be writing this post as somebody who comes from a C# background and has also used F#. So, sorry if you’re reading this and thinking “I don’t know what F# would look like” for example.

val and var

A val is immutable whereas a var is not. Hence assuming we have the following

object Calculator {
  def add(a: Int, b: Int): Int = {
      a + b
  }

  def subtract(a: Int, b: Int): Int = {
    a - b
  }
}

The we can assign our val thus

val calc = Calculator

// this line will error
calc = Calculator

whereas our var will allow reassignment, hence

var calc = Calculator

// this line will success
calc = Calculator

Simple enough.

Types

I’m not going to list all the types supported in Scala – ofcourse we have Int, String etc. What I will say though, is like F#, a type may be inferred or we might specify the type where a hint is required

Let’s see how we can use a type

val inferredString = "Hello"
val explcitString : String = "Hello"

We declare a type after the name of the val/var, as per the example above.

There’s an ultimate base type in Scala (a little like object in C#) and this is called Any. According to Unified Type System this Any type is not the same as the java.lang.Object. In Scala we have AnyRef which IS analogous to java.lang.Object whereas AnyVal is the equivalent base of value types. Ofcourse Any is the base of both AnyValue and AnyRef.

Like F#, Scala prefers the idea of a Nothing value than a null but (again like F#) null is also supported.

Scala, ofcourse, supports collections and arrays.

We can declare and array with type expected type or inferred, for example, these two arrays create the same type of array

// explicit
val a = Array[String]("a", "b")
// inferred
val b = Array("a", "b")

To access/alter an array at a given index we again use brackets (), i.e.

val a = Array("a", "b")
// amend "b" to "c"
a(1) = "c"

Return values

When a method returns a value we can use the return function, for example

def result : Int = {
   return(123)
}

or without parenthesis, i.e. return 123 or, like F# the return can also be the last line of the function, i.e.

def result : Int = {
   123
}

and with the type being inferred we can reduce this further to

def result = {
   123
}

Loops

The for loop, using the to function, for example

for(i <- 1 to 3) {
   print(i);
}

// will print
// 123

The for loop, using the until method

for(i <- 1 until 3) {
   print(i);
}

// will print
// 12

to and until are methods, hence we could use the following syntax

for(i <- 1 to 3) {}
for(i <- 1 to.(3)) {}
for(i <- 1.to(3)) {}

each of the above does the same thing, just with different syntax. What each of these methods does is return a Range, for example, in the the following code r is of type Range

val r = 1.to(3)

for(i <- r) {
   print(i)
}

It’s always worth noting the i is a val (hence immutable) type and is created each time through the loop.

Note: So how can “to” and “until” be used without parenthesis? If a method is zero or one arguments, we do not need the parenthesis. Therefore, if we have code like this

object Calculator {
  def inc(a : Int): Int = {
    a + 1
  }
}

// we can do this

val calc = Calculator
val r = calc inc 2

Back to loops, so we have a for loop, we’ve also got while loops so

var i = 0
while(i < 3) {
   print(i)
   i = i + 1
}

as well as do…while

var i = 0
do {
   print(i)
   i = i + 1
} while(i < 3)

like C#/Java we can also break out of a loop, however this keyword is not part of Scala. To use, we need to import the functionality using

import scala.util.control._

var i = 0
while(true) {
   print(i)
   i = i + 1
   if(i == 3)
      Breaks.break
}

Importing functionality/libraries

As you’ve seen with the Breaks.break method, we can import functionality as we do in C# with using and import in Java, like this

import scala.util.control._

The use of the _ is like a saying import everything within the scala.util.control namespace and we’ll then use code like Breaks.break to call the method, but we could also be more explicit and change our code to be like this

import scala.util.control.Breaks.break

// other code
if(i == 3)
   break

Like F# being built atop .NET and therefore allowing us access to a rich framework and eco-system, Scala is built upon Java and hence allows us access to it’s rich set of frameworks etc.

import java.util.Date

def output  = print(new Date() + ":" + msg)

in the above, it’s probably obvious, we’re using the Java Date class, we can import multiple specific classes like this

import java.util.{Date, Locale}

or for all (as already seen)

import java.util._

Classes and Objects

We can define a class as follows

class Report(msg: String) {

  def output = println(msg)
}

By default (i.e. without an scope declaration) classes are public, we could prefix the class thus private class Report to change from the default.

The msg: String is like a constructor (a similar design came up for Primary constructors in C# 6) but we can also create overload for the constructor like this

class Report(msg: String) {

  def this(msg: String, num: Int) = this(msg + "  " + num.toString)
  def this(num: Int, msg: String) = this(msg + "  " + num.toString)

  def output = println(msg)
}

We can also define an object like this

object Report {

  def output(msg: String) = println(msg)
}

In scala this is known as a singleton object, I would say similar to a static class in C#. Hence does not have a constructor as we cannot create an instance of Report and we do not use the new keyword to use it, i.e.

val s = Report
s.output("Hi")

It’s also possible to associate an object with a class, for example

class Report(msg: String) {

  def this(msg: String, num: Int) = this(msg + "  " + num.toString)
  def this(num: Int, msg: String) = this(msg + "  " + num.toString)

  def output  = Report output(this.msg)
}

object Report {
  def output(msg: String) = println(msg)
}

This pairing of class with an object is known as a companion class and vice versa we can think of the object as the companion object. In the example above, we’ve defined the equivalent of a static method in the object and the class output calls this. Obviously this is a slightly convoluted example, but another use is that the object might include (for example) private fields which are used from the class, like this

class Report(msg: String) {

  def this(msg: String, num: Int) = this(msg + "  " + num.toString)
  def this(num: Int, msg: String) = this(msg + "  " + num.toString)

  def output  = print(Report.name + ":" + msg)
}

object Report {
  private def name = "Report"
}

alternatively, we can import the object and remove the need to prefix use of name with Report – like this

class Report(msg: String) {

  import Report._

  def this(msg: String, num: Int) = this(msg + "  " + num.toString)
  def this(num: Int, msg: String) = this(msg + "  " + num.toString)

  def output  = print(name + ":" + msg)
}

object Report {
  private def name = "Report"
}

Commenting code

As you’ve seen in some of the examples, scala supports // for single line comments as well as the standard (at least in C style languages) /* */

We can also support scaladoc comments, these use /** */ for example

/**
This is a report
*/
class Report(msg: String) {
}

In Intelli> (for example) selecting the Report when used in our code and pressing ctrl+q will result in a popup which will display the documentation for the class, the text with /** */ will be displayed as documentation.

Whoa, hold on, how about an application

So for all of the above, I’ve talked about the language, for these examples I’ve used ScalaTest but what about an entry point for an application.

Ofcourse we have main and we can create our application object like this

object MyApp {
  def main(args: Array[String]): Unit = {
     println("Hello World")
   }
}

As you’ll recall from the section on classes and objects, this is similar to a singleton class with, what we might view as, a static method.

This requires the args : Array[String]) and must return a void/Unit