Friday, December 11, 2015

DateMatchers for unit testing a Date object

Hey, there is no DateMatcher in the HamCrest! Why?

I found a useful library: hamcrest-date. Really cool for unit testing:

assertThat(dola.getTranslationStatus().getResultDate(), is(isToday()));
assertThat(dola.getTranslationStatus().getResultDate(), is(sameDay(new Date())));


Wednesday, December 9, 2015

java Date and equals()

The system receive a Date object from the UI after the JSON conversion, than he try to find the related record in the database and compare the changes.

We use Jackson JSON with Sping annotation on REST API, and Sping Data to read the database.

We found the Date objects are instance of java.util.Date and other extensions. In this case the java.util.Date.equals() is not useful, he could not evaluate because of the different date representation.


If you would like to use the checking you have to use the compare() function. And also modify the generated equals() function of  your Class:

if (publishDate == null) {
if (other.publishDate != null) {
return false;
}
} else if (other.publishDate == null || 0 != publishDate.compareTo(other.publishDate)) {
return false;
}

Tuesday, November 17, 2015

Autowired beans in a web application

We started a new project in the previous Spring. And we chose Spring framework to manage many functionality, we were so lucky to make this decision.


The Spring annotations are very calm and comfortable. We created a configuration class:

@EnableScheduling
@Configuration
@ComponentScan(basePackages = {
"org.whatever.sthg",
"org.whatever.sthg.util"
})
public class BeansConfig {
....
}

With the @Configuration annotation you can mark your class, it contains the configuration.
With the @EnableScheduling you can define late chron like jobs.
With the @ComponentScan you can list your packages where Spring framework could find the @Autowired, desired beans.

For example a @Controller and an autowired @Component class:

@Controller
public class CommonController {

@Autowired

private DomainFacadeInterface domainFacade;
...
}

@Component
public class DomainFacadeImpl implements DomainFacadeInterface {...}


The entry point should be an xml file, or static main() function to bootstrap Spring framework.
We decided to use the web.xml with the following extra lines:

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            org.whatever.BeansConfig
        </param-value>
    </context-param>

You can forget to maintain massive XML files.

Monday, November 16, 2015

How to log Axis messages

If you use webservice calls in your system, the axis has very common 3rd party libraries to send and receive the messages: SOAP, XML, JSON whatever.

Sometimes desirable to see in the logs what kind of messages travel in your system.


Just create the following XML file, named: client-config.wsdd
Put the file to any resource folder. After a new deployment you will see the messages on the console.

I tried to log the messages to a log file, it was not working for me. But i did not want to spend more time with this analysis.

<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig" xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

   <handler name="log" type="java:org.apache.axis.handlers.LogHandler">
<parameter name="LogHandler.writeToConsole" value="true" />
   </handler>

   <globalConfiguration>
      <parameter name="disablePrettyXML" value="false" />
      <requestFlow>
         <handler type="log" />
      </requestFlow>
      <responseFlow>
         <handler type="log" />
      </responseFlow>
   </globalConfiguration>

   <transport name="http"  pivot="java:org.apache.axis.transport.http.HTTPSender" />
   <transport name="local" pivot="java:org.apache.axis.transport.local.LocalSender" />
   <transport name="java"  pivot="java:org.apache.axis.transport.java.JavaSender" />
</deployment>

Wednesday, September 2, 2015

Connection Failure message in ColdFusion REST calls

Story

After a while we and the user detected some queries on the user interface are finished with a strange message: "Connection Failure" with HTTP 200 OK. Who generated this message, why we received this? Why HTTP 200? Only in Production environment.

The message appeared in only every 10th call or later, but after the first strange answer it was constant until a half minute.

This system architecture is very simple, this is the scheme:


What is the cause?


1. Performance issue?

After a test period we contacted with the support, they figured out they will increase the memory in the weblogic server.

Of course It was not enough.

2. Communication problem?

We logged the request and response headers and body to analyze the situation. We checked also the weblogic logs about the response size of the same queries.

User-Agent: ColdFusion
Accept-Encoding: identity
Cache-Control: max-stale=0
Connection: Keep-Alive

The responses from the Weblogic server were perfect every time, but we still had the problem on CF.

3. Environment problem?

There was a dummy idea for testing the environment, we have development, test, acceptance, load and production.

As mentioned we changed the configuration pointing a Weblogic to another database, and CF server to another Weblogic server. In some environments we use HTTP, in others HTTPS.

This test was not useful for the analysis.

4. Check the architecture again

Look again the system architecture deeper:

Ok. Of course we have more IPs, servers, firewalls, proxies etc!

Some testers started to call the proxy IP instead of the  Weblogic IP. We saw the response header was different sometimes, they change totally the Weblogic response and compress the data, against the CF headers, which contains in the Accept-Encoding, give me uncompressed data.

After a little search we found, the ColdFusion server sometimes can not handle the compressed/gzipped response, he says "Connection Failure"  with HTTP 200 OK.

Content-encoding: gzip

Solution

Switch off the data compression in the proxy, in our case.

Conclusion

The application was perfect, the solution was working until a point. Somebody changed the configuration of one element of the environment, and we faced with this communication issue.

The ColdFusion server was not user and support friendly, again. He got the messages, the content well, the HTTP 200 was true, but the "Connection Failure" message was false. There was no problem with the connection, the CF could not compress or handle the gziped data.

Tuesday, June 9, 2015

CDATA extension in XML feed with annotation

If you use javax to generate XML content, sometimes you use special characters. Better to place this data into <![CDATA[ ... ]]> tags.


Using annotations are very comfortable.

  1.  Create an adapter class: AdapterCDATA
  2. Add @XmlJavaTypeAdapter annotation to the field

Create the adapter class

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class AdapterCDATA extends XmlAdapter<String, String> {

@Override
public String marshal(String arg0) throws Exception {
return "<![CDATA[" + arg0 + "]]>";
}

@Override
public String unmarshal(String arg0) throws Exception {
return arg0;
}
}

Add the annotation

@XmlRootElement(name = "PressReleaseLinguistic")
@XmlAccessorType(XmlAccessType.FIELD)
public class PressReleaseLinguisticXmlVO implements Serializable {

private static final long serialVersionUID = 3000191037371295127L;

@XmlElement(name = "pdf")
public String pdf;

@XmlJavaTypeAdapter(AdapterCDATA.class)
@XmlElement(name = "html")
private String html;
.....

The result

<PressReleaseLinguistic>
<html>
<![CDATA[<p class="A__35__20_Normal_P5">News from the European Commission"s Midday Briefing</p>]]>
</html>
<language>EN</language>
<title>Sample title</title>
....

Thursday, June 4, 2015

Prevent logging sensitive data

Prevent logging to the user interface sensitive data, especially when you create a Web-shop, or the service is connected to a bank system, or your session is related to money.

Many cases you can find details about the system. These details are useful for hackers to create confusion in your service.



In this screenshot you can find many data about the service:
- used technologies
- version of the used framework
- transaction, session details: ids
- source code details, function names for example: btnPayWithCreditCard_Click()


If the exception is repeatable, the unfriendly user can analyze your service.

Better to create a simple error code, and if it occurs the proper service user, support team could check in the documentation how to get the details of the error.

Saturday, May 30, 2015

Understanding Big Data


The Dataconomy Team published a book for beginners https://www.smashwords.com/books/view/498197. The title is Unserstanding Big Data.
They have also a news and job portal in the topic. 

You can get this book as a gift if you take part in this survey: https://ftjournal.typeform.com/to/FFYCuk 

I've just downloaded, it seems to be an overview, maybe useful for managers with some use case. good luck.

Wednesday, May 27, 2015

Using rownum and order-by

If you try to use rownum and order-by in a select statement, maybe the result will not match with your expectation. It is because the order-by is one of the last operation in the queue.

Using rownum function


The query:
SELECT ROWNUM,
       T.DOMA_KY,
       T.SHORT_DESCRIPTION_LB
  FROM T_DOMAIN T
 WHERE T.ACRONYM_CD = 'PLCY'
 ORDER BY T.DOMA_KY


    ROWNUM DOMA_KY SHORT_DESCRIPTION_LB
1 6 261 P1-Jobs, Growth and Investment
2 2 262 P2-Digital Single Market
3 4 263 P3-Energy Union and Climate
4 8 264 P4-Internal Market
5 3 265 P5-Economic and Monetary Union
6 10 266 P6-EU-US Free Trade
7 7 267 P7-Justice and Fundamental Rights
8 9 268 P8-Migration
9 5 269 P9-EU as Global Actor
10 1 270 P10-Democratic Change

Using row_number() function


The query:
SELECT ROW_NUMBER() OVER (ORDER BY T.DOMA_KY) ROW_NUMBER,
       T.DOMA_KY,
       T.SHORT_DESCRIPTION_LB,
       T.ROWID
  FROM T_DOMAIN T
 WHERE T.ACRONYM_CD = 'PLCY'
 ORDER BY T.DOMA_KY

    ROW_NUMBER DOMA_KY SHORT_DESCRIPTION_LB
1 1 261 P1-Jobs, Growth and Investment
2 2 262 P2-Digital Single Market
3 3 263 P3-Energy Union and Climate
4 4 264 P4-Internal Market
5 5 265 P5-Economic and Monetary Union
6 6 266 P6-EU-US Free Trade
7 7 267 P7-Justice and Fundamental Rights
8 8 268 P8-Migration
9 9 269 P9-EU as Global Actor
10 10 270 P10-Democratic Change

Wednesday, May 20, 2015

Force java project import to Eclipse

There are several way to import an existing code, folder as a java project to Eclipse.


These are very useful solutions if your project is built by not maven:

  • create a new Eclipse project with a same source folder. Sometimes you get an overlapping error.
  • create a new Eclipse project from an existing ant build file. Specially the Eclipse try to parse and figure out which ant target is the best to compile your project during the import. Unfortunately the Eclipse try to find "javac" ant calls in your build script.
  • try import only the java folders of the projects
  • try to import as an existing project, and maybe the Eclipse will recognize
  • create a .project file and enjoy
to create a .project file is very easy, you can copy an old one. Or create a file like this:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Pecunia</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>

Friday, May 8, 2015

Create a design for WebServices

I tried to find a really easy and good tool to create a design for Web-services.

Maybe I  did not spend enough time to find one, so I chose again my favorite yEd.

The yEd is a very lightweight and useful application for drawing, creating boxes with attributes, relations, colors, grouping.

The artifact of the yEd is an XML. It is very easy to modify, to maintain, to load it to the repository.

So you can create Entities with this tool, just drop it from the Entity relationship part of the Palette. You can add attributes, and you can also define relations.

What is the benefit to have a web-service design?

Firstly, you will have a documentation. You can share the current functionality, the entities. If you leave the project, the newcomer colleague can use it.

Secondly you has an overview. It is not easy to follow the XML files, where the WS is defined, and also not easy to parse the java objects, where the annotations are, which filed is the part of the XML element.

Thirdly you get a review. I found with my little WS two little thing, when it was modified, the coder forgot to follow the naming conventions. After that I found a bug in the Java objects, in the annotations. The sub entity name was different in the Entity java class, and in the parent java class. Despite of this bug the WS also worked.

An example:

Rename maven projects in the Eclipse

I am working on old project, with old code. The problem is, if you want to import the full maven project hierarchy you will be faced a name conflict.

It is not a big issue, but you can not import the two project in same time.

What is the logic to give the project name in the Eclipse?

The Eclipse use the folder name for the root project, and the Artifact Id for the children maven projects.

The solution

There is a solution in the Eclipse, when you stat the import with the wizard, you can select a predefined template to rename the Eclipse project name. You can find it under the advanced label in the first step of the wizard.


Let's start a blog

Let's start a blog, just to keep in mind my daily IT challenges. If it will be successful, I will increase my English and I will have a good collection of solutions IT problems.

If you would like to help me, welcome!

When I start this blog I have more than 15 years experience in IT development, consultancy and design.

You can find here solution with Java, Oracle, JavaScript, ANT, MAVEN, Shell, Perl, Regexp, XML, RSS, REST, PL/SQL etc.