2013-11-30

Java RESTful Web Services, NetBeans Style

I’ve been interested in exploring Java RESTful Web Services to backend some AngularJS front-ends, with my current focus on JAX-RS implementations.

Blaise Doughan has been blogging a lot about EclipseLink, JAXB, and MOXy. I decided to follow the code example in his post MOXy is the New Default JSON-Binding Provider in GlassFish 4 using NetBeans 7.4 since the Java EE download bundles an install of GlassFish Server Open Source Edition 4.0.

Start by creating a new Java Web Application by choosing New Project from the File menu, going to the Java Web category and selecting Web Application. To keep things the same as his example, name it CustomerResource. Select the GlassFish Server 4.0, Java EE 7 Web, with the suggested context path of /CustomerResource. If you run this right away you should be served the index.html page saying “TODO write content”.

We will work backwards a bit in his blog post, building a little infrastructure before we use it. So first we will right-click Source Packages and add a new Java Class named PhoneNumber in the org.example.model package. Paste or type his code into this class. Do the same for the Customer class. NetBeans will suggest you use the diamond inference and make phoneNumbers final. The code works fine either way.

Still working up in the blog, we will create the CustomerApplication and CustomerService classes in the org.example.service namespace. At this point you should be able to click run and visit the local URL to get our “hello world” type xml response for Jane Doe:

http://localhost:8080/CustomerResource/rest/customers/1

Everything up to this point “just works” in the excellent NetBeans IDE and GlassFish Server, but I was interested in his JSON tweaks, having seen some of the shortcomings he mentions unless I map to a JSON object by hand. To do some testing I first commented out the APPLICATION_XML line from the @Produces list so that I could see (download) the output and move forward to Customizing the JSON-Binding with it’s use of MoxyJsonConfig. This is where I was stumped for a bit.

Pulling in the JAXBContextProperties wasn’t a big deal. The EclipseLink from GlassFish library seemed to have what I was after. Just right-click the Libraries folder in the project and Add Library then choose that library and click Add Library.

To get MoxyJsonConfig, download jersey-media-moxy-2.4.1.jar and stick it someplace handy. I use a folder named Libraries in my NetBeansProjects folder. Then right-click the Libraries folder in the CustomerResource project in NetBeans and click Create in the Add Library dialog. Name it something like Jersey Media Moxy and then in the library classpath Add Jar/Folder to add jersey-media-moxy-2.4.1.jar. Then add this library to your project.

At this point you should have output like Blaise has documented for New Response in his blog post. Enjoy.

If you can’t find jersey-media-moxy-2.4.1.jar or the API has switched around again and a later version is missing the dependency, then read on for my tale of woe and sorrow trying to locate it in the first place. Perhaps it will help.

I haven’t jumped on board with maven yet, so when I came across Blaise’s follow-up question to the StackOverflow question Cannot import EclipseLink MOXy while searching for MoxyJsonConfig where he implied the use of Maven, I was a little disappointed. I was equally disappointed in my next dozen searches all failing to find the jar containing MoxyJsonConfig. I could find API docs, people talking about using it, etc. findjar.com failed. Even mvnrepository.com searches failed. Finally after Google searches of varying portions of the class or package name, one for org.glassfish.jersey.moxy pointed me to jersey-media-moxy within MVN. Unfortunately it was pointing me to 2.0-m07 which has MoxyJsonConfiguration and not MoxyJsonConfig. I didn’t realize that right away and tried implementing using it. It doesn’t work. The current latest version, 2.4.1, has MoxyJsonConfig and does work. I have no idea when things changed or what version Blaise used.

I was glad to finally find the jar, but there has got to be some better way to find a class and know what version of things people are talking about. If there is, please share. If there isn’t, please keep this in mind when sharing code examples.

2011-01-01

Scratch - programming in disguise

My memory of my introduction to programming is a little fuzzy, but a few notable exposures stand out. In no particular order they were ChipWits, TurboTurtle/Logo, World Builder and a BASIC based program that controlled a little submarine which it seems was tongue-in-cheek called GoSub. Each had it's own reason for sparking some interest in figuring out the logic of a program and each had it's quirks.

I think I spent the most time with ChipWits, an old version for the early Macintosh (Were we up to a "512"(K) or a Plus by this time? I am not sure.) It's simplistic IBOL interface for graphically coding the actions of the little computer on roller skates was easy to grasp, and the sounds and animations as it touched, tasted, ate and zapped (or got zapped) were entertaining.

The TurboTurtle intro was a brief after-class exercise while doing some kind of introduction to computers evening class. I mostly remember pen up, pen down, and seeing the computer screen draw patterns like you could make with a spiral sketch tool.

World Builder started out as a game to play (playing games created with it) but then I wanted to try and make my own story/game. Even though it inspired me to think a lot about the story and programming, I never got more than a basic grasp of the language as I tried learning from other code without a manual. Another hurdle for me was not being particularly artsy so the graphical aspect of the game was pretty lacking.

The "GoSub" program was fun. It seems like it was a program/IDE and a book to teach some programming. I was typing in some actual code that was calling all the drawing routines and it felt like quite an accomplishment when I could actually get the sub to dive and surface and fire a torpedo. The program would have one of those crushing sad mac error messages when the torpedo would hit the target. After a while of not figuring that out I gave up on it.

Looking back and summarizing my experiences, my spark of interest to learn how to program the computer wasn't started by looking at code and thinking "wow, 'declare i integer' looks really amazing. I want to type that all day." No, It was the desire to create something fun that I could then play with or show off to others. Programming was the necessary evil for my desired end.

I want to provide the opportunity for the same spark to code within my own children. Sure, they may not be interested in being programmers, but they will be interacting with computers a lot and it can be helpful to "know how the engine works". Besides, the problem solving and logic skills can be applied to many aspects of life.

At first I thought I could take something like GNU Robots and wrap it in a ChipWits like interface. That turned out to not be a quick task and lead me down the road of exploring other robot programming concepts, most frequently the "robot battle" and even LEGO Mindstorm NXT. It seemed a little too advanced to keep their interest for now, so I looked some more.

There is a lot of buzz about a couple of drag-and-drop programming systems, Scratch and Alice, for introducing programming concepts and empowering people to be creative with their computers.

After watching some videos about Scratch and even more impressively, downloading and playing community contributed programs I'm hoping that my more creatively minded kids will catch some of the vision of how they could use Scratch to express themselves through animated comics (even better than my old friend Comic Strip Factory,) music videos, drawing random patterns (TurboTurtle), and arcade (GoSub) or role playing style games l(World Builder).

The Scratch community gives a lot of opportunity for sharing your strengths to build onto a started project (remixing) or join a team and to show off your works. I think that is a very powerful aspect.

If they push the limits of the Scratch 2d world and are willing to get a little more technical to go 3d, they can step up to Alice and do "Word Builder in 3d". Who knows, maybe one of them will really get into it and be willing to work through How To Design Programs and/or Python Programming: An Introduction to Computer Science or some Python tutorials. Maybe after 10 years they will start to be a really good programmer.

The trick is to get them started and keep it fun and consistent and the best way to do that seems to be for me to do it with them.

2010-01-08

Rethinking Nagios config layouts

I've been using Nagios 1.4 in Debian Etch for some time now. By default they set you up with a working layout like this:

nagios.cfg:
...
cfg_file=/etc/nagios/contactgroups.cfg
cfg_file=/etc/nagios/contacts.cfg
cfg_file=/etc/nagios/dependencies.cfg
cfg_file=/etc/nagios/escalations.cfg
cfg_file=/etc/nagios/hostgroups.cfg
cfg_file=/etc/nagios/hosts.cfg
cfg_file=/etc/nagios/services.cfg
cfg_file=/etc/nagios/timeperiods.cfg
...

The services.cfg file started out with a generic-service template where you set the standard options most generic-service services will be using, and then a list of services. A service definition must contain, among other things, the name of the host or host_group that the service runs on. So each time I setup a new host, I would enter it in the hosts.cfg, hostgroups.cfg, and add all of it's services in services.cfg. I was using hostgroup objects just for display and contact_groups and not taking advantage of them in service objects. Those two things combined made for a long services.cfg file. At least I was taking advantage of a couple of service object templates.

The nagios3 config layout shipped with Debian 5.0 (Lenny) opened my eyes to a new structure. It uses the cfg_dir directive to point to /etc/nagios3/conf.d and in there we have:

contacts_nagios2.cfg
extinfo_nagios2.cfg
generic-host_nagios2.cfg
generic-service_nagios2.cfg
host-gateway_nagios3.cfg
hostgroups_nagios2.cfg
localhost_nagios2.cfg
services_nagios2.cfg
timeperiods_nagios2.cfg

In the localhost_nagios2.cfg file they define a simple host that inherits from geric-host and a few extra services, rather than putting localhost entries into the services config file. Checking out services_nagios2.cfg I found a bunch of generic (eg check_http, check_ssh, check_ping) services using host_group names instead of hosts.

Wandering over to hostgroups_nagios2.cfg I found a bunch of very simple hostgroup object definitions. Some like http-servers listed localhost as one of their members. If we reverse that situaion though and drop the hostgroup.members declaration and instead set host.hostgroups to include http-servers, then if I need to make customizations to localhost or delete it alltogether, I do it all in/to localhost_nagios2.cfg. One file. I like this much better!

Nagios' object based configuration system is very flexible. I don't think any of this stuff is new to version 3. I've read the docs dozens of times but it took a fresh install with a new layout idea, some mistakes on my part and some help from Marc on nagios-users to get it sorted out in my head and on disk. Once I had that, the migration of other hosts to the nagios3 setup has been fast and minimal.

2009-12-01

Threshold filter & what about RDF Annotations

I hope Google Sidewiki has a good threshold filter so we can vote down drivel and not have to see it. Browsing through the first several pages of the Google Sidewiki Sidewiki showed me this neat tool is already getting messy on a high-traffic page, and here I am adding to it. ;)

I've been waiting to try a mature RDF Annotations solution as proposed in the Amaya browser documentation for Annotations. A kind of mix between Google Sidewiki and Google Notebook.

in reference to: Google Sidewiki (view on Google Sidewiki)

2009-06-15

Linux VServer & PAM limits

I've been getting some setrlimit "Security Events" reported by logcheck the past few weeks from a Debian 4.0 (Etch) virtual host running on a Debian 5.0 (Lenny) host. I was busy when they started and didn't drill down to see if it started with a security update in one system or the other.

Jun 15 10:09:01 green pam_limits[4284]: setrlimit limit #11 to soft=-1, hard=-1 failed: Operation not permitted; uid=0 euid=0 Jun 15 10:09:01 green pam_limits[4284]: setrlimit limit #12 to soft=-1, hard=-1 failed: Operation not permitted; uid=0 euid=0

Today I did some searching and found a few related posts ranging from bugs in sshd's limit setting to Linux-Vserver specific cases.

  • http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=171673
  • http://www.mail-archive.com/vserver@list.linux-vserver.org/msg08783.html
  • http://osdir.com/ml/linux.pam/2007-09/msg00024.html

One suggestion from the Linux-Vserver list was to just comment out the use of pam_limit in the pam configuration files. I didn't want to throw the baby out with the bath water so I decided to take their other suggestion and modify the /etc/security/limits.conf file.

First I had to find out what limit #11 and #12 were on my system. I'm running a 686 kernel so I looked in include/asm-generic/resource.h of the matching kernel source. There I found that 11 was RLIMIT_SIGPENDING and 12 was RLIMIT_MSGQUEUE. Before getting there I had grepped around and noticed that the alpha and mips architectures swap a few values around so you may need to look at more than the generic header.

Next I used the ulimit command to list the values of pending signals (-i) and POSIX message queues (-q) applied to a default user session in the host system. I modified the /etc/security/limits.conf to set the hard and soft limits for these two resources to match. This stopped the setrlimit messages in /var/log/auth.log for new ssh logins, but they remained when cron attempts to run as root even after restarting cron. The same happens if I allow root ssh logins and login as root.

  • http://linux.derkeiler.com/Mailing-Lists/Debian/2008-08/msg01096.html

2009-03-19

PuTTY X11 forwarding stopped working on Vista

At work my primary desktop system is running Windows Vista Business 64 and our servers are all Debian. Almost daily I access these servers using ssh via the awesome PuTTY (0.60) program. When I want some graphical display returned, like a gvim, pgadmin3 or etherape, I fire up Cygwin/X and use it as my local X server. Once configured this has been working flawlessly for years on MS XP and Vista the past several months.

Yesterday when I tried firing up etherape to track down some network usage, I got the discouraging message "X connection to localhost:10.0 broken (explicit kill or server shutdown)." I followed some basic ssh and X forwarding troubleshooting steps and everything checked out. Finally I found and installed iftop which was everything I wanted ntop to be and did a good enough job.

Today I tackled the issue and found that a Cygwin/X xterm would properly bring the xclient home to my desktop, so the issue was not the servers (which had not had any software changes in the previous few days) it must be the software running on Windows.

I looked at the PuTTY > Connection > SSH > X11 settings and noticed the X display location was blank. I thought this might be akin to the $DISPLAY variable so I checked the help file. It said that if the field was blank PuTTY tried to use :0. Sounded reasonable, but I decided to test it anyway. The xterm had said $DISPLAY was 127.0.0.1:0.0 so, prefering names over numbers, I tried localhost:0.0. No dice. I then tried 127.0.0.1:0.0 and was rewarded with xclock and friends from the remote system.

A little googling lead me to a bug report from 2004 about possibly needing to specify the X display location. I don't think my issue was the same. The note claims that using localhost:0 on MS Windows should work, and it does not for me. I believe that some recent Windows Vista update or setting change (I have not changed networking settings for months) has broken localhost:0, but somehow using the ip address instead of the name works from PuTTY. It doesn't matter if I use the number or the name in a local Cygwin xterm.

Next I pulled up the trusty cmd.exe and tried ping localhost. This showed me that localhost resolves to the IPv6 address ::1. Perhaps this has changed recently due to a Windows update or some setting change I don't remember? I don't know if the root issue is Cygwin/X server not listening on IPv6 addresses (perhaps my version isn't even capable) or if the IPv6 syntax doesn't play nice with the final :0. I did try setting the display to ::1:0 in the xterm and that didn't work. It said "_X11TransSocketINETConnect: Can't get address for :: Error: Can't open display: ::1:0".

For now I'm going with updating all my PuTTY session configs to use 127.0.0.1:0.0

2008-11-13

LARTC the Debian Way (tm) - Intro

I really appreciate the flexibility and plethora of information you can find on open source topics, especially high visibility items like the Linux Kernel and tools to configure it's run-time state. One collection of tools that has helped me find creative solutions to a number of networking challenges has been the set described in the Linux Advanced Routing & Traffic Control HOWTO (LARTC). What gems the iproute2 (sometimes just iproute) package holds. After I've gone through and whipped up a solution via the ip and tc commands, I start to wonder about how to get it to survive through a reboot. The servers I work with are primarily running recent versions of Debian so when I come across and implement solutions like the custom script I came across at minigeek.org I wonder how to set up these things in "the Debian Way" (tm). I'll make follow-up posts as I find solutions.