Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Saturday, March 5, 2016

XML parsing in Java

JSON is much popular now, but occasionally it is still possible to come across XML API. I had such experience recently and I have to say that in 2016 it is much easier than some 10 years ago. I mostly use Jackson for JSON, so for me best way to use it is XmlMapper plugin. After that it is plain Jackson.
There is example:


XmlMapper mapper = new XmlMapper();
List rates = mapper.readValue(ratesString, List.class);




compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.6.3'
compile 'org.codehaus.woodstox:woodstox-core-asl:4.4.1'





Thursday, February 13, 2014

Storing sessions in Redis with Spring Boot

Tomcat has nice support to use Redis for session replication with this awesome library. However Spring Boot launches embedded Tomcat, so there is no traditional XML configuration, it is still super easy to change default manager to use Redis for session replication, just define containerCustomizer bean, like:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class App {
  public App() {
  }

  @Bean
  public EmbeddedServletContainerCustomizer containerCustomizer(){
    return factory -> {
      TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) factory;
      containerFactory.addContextValves(new RedisSessionHandlerValve());
      containerFactory.setTomcatContextCustomizers(Arrays.asList(context -> {
        context.setSessionTimeout(30);
        context.setManager(new RedisSessionManager(){{
          setHost("redis.server.com");
        }});
      }));
    };
  }

  public static void main(String[] args) throws Exception {
    SpringApplication.run(App.class, args);
  }
}

(this is syntax with new shiny Java 8 lambdas, but with few additional boring types it should compile in old Javas too).

Wednesday, October 24, 2012

List in groovy-wslite request

Groovy-wslite is currently recommended way for doing client calls to web services from groovy. It has very interesting format for describing requests, unfortunately it is not very well documented, and for me it was totally unclear how to send lists of objects to service. Fortunately, source code is available and is easy to dig if you have some time.
Basically, all you need to do is just send a bunch of regular objects, and they will be combined in list on the other end. There is example:

def response = client.send(SOAPAction:'https://server/services/service') {
  body {
    create() {
      dto {
        name('name123')
        id('123456')
        child {
              name('child1')
              birthday('2011-01-01')
            }
        child {
              name('child2')
              birthday('2012-01-01')
            }
      }
    }
  }
}


Friday, September 28, 2012

Using Spring Security with CXF in Grails

CXF plugin is super easy way to add SOAP web service to Grails application. It creates regular service class, with few specific parameters and voila - you have SOAP. But what if you want security?
REST web services are native citizens in Grails - they are just actions in controllers, so you can use Spring Security annotations to check permissions. But there is no annotations for services. There are two easy options.
First, you can restrict actions by defining static rules. This is very simple, but not very flexible as it does not allow to configure permissions on method level, you will have to create different services for different permissions. Also, old clients may not work if WSDL requires authentication.
Second method, is to check permissions manually. In practice this is similar to annotations, and can be converted to annotations easily. There is example:


  Secret secret(int id) {
    checkRights("VIEW_SECRET")
    return Secret.get(id)
  }

  void checkRights(String rights) {
    if (!SpringSecurityUtils.ifAllGranted(rights)) {
      throw new IllegalAccessException("You don't have permission")
    }
  }

Wednesday, October 19, 2011

JUnit report formatting in Grails

Sometimes it is needed to add some additional information to JUnit report. For example, if you want to add screenshots and so on. It can be easily done in Grails.
First, you need to modify scripts/_Events.groovy and redefine reporting template folder.

eventAllTestsStart = {
  junitReportStyleDir = "src/templates/reports/"
}

Then create src/templates/reports folder and just add junit-frames.xsl and junit-noframes.xsl from lib folder under Grails home.

Now Grails will use these files to format test results.

Friday, April 1, 2011

Skip XML declaration in JAXB

By default JAXB outputs XML declaration when you marshal your objects:
For example, this:

JAXBContext context = JAXBContext.newInstance(Command.class, Message.class...);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(command, writer);
will output this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<message channel="test"/>


If you don't need to use XML declaration in your JAXB output, it is easy to skip it by setting property "jaxb.fragment" to true. So, this:
JAXBContext context = JAXBContext.newInstance(Command.class, Message.class...);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
marshaller.marshal(command, writer);
will output only this:
<message channel="test"/>