Tuesday, November 18, 2014

Friday, November 14, 2014

Linux Enable chkconfig

After creating a script in "/etc/init.d" to start/stop/restart a service, you will still need to use chkconfig to ensure that the process starts during server bootup.

To enable chkconfig, add the line in red at the top of the script in "/etc/init.d/"
#!/bin/sh

# chkconfig: 35 90 10

start(){
       # start the service here
}

# --- snipped ---


  • "35" means the service will be activated for runlevels 3 and 5
  • "90" means that the service will be started at priority 90 (bigger is later)
  • "10" means that the service will be stopped at priority 10

Tomcat Enable Self-Signed SSL

Assuming Java tools are already on your PATH:

As root user:
keytool -genkey -alias tomcat -keyalg RSA

Then answer the questions accordingly

Reference: http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html

Linux Set Time Zone

Third method on this page: http://www.wikihow.com/Change-the-Timezone-in-Linux

e.g.


ln -sf /usr/share/zoneinfo/Singapore /etc/localtime

Thursday, November 13, 2014

Linux Putting Process Into Background

When you are running a process that takes a long time and you need to logout of the server, and if you didn't run it with the "&" at the end to run it in the background, you can still send the process to the background with the following steps:

  1. CTRL-Z (This will give you your bash prompt back)
  2. If you type "jobs" you will be able to see the list of jobs (YMMV but what I saw was my process listed as "Job 1" but it was stopped)
  3. To resume it in the background, I had to tell issue the command "bg 1" (or whatever the job number is)
  4. You can also move it back to the foreground with "fg 1"

Wednesday, November 12, 2014

Monday, November 10, 2014

Enabling CORS With Cookies on Jersey

This previous post shows how to enable basic CORS functionality in Jersey. However, to enable cookies and authentication, more needs to be done. First of all the "Access-Control-Allow-Credentials" header needs to be set, then the other issue is that a wildcard "*" cannot be used with "Access-Control-Allow-Origin". You will either have to specify in hard-code the origins that you will support, or you'll have to use the Origin in the request for this response header.

Sample code:

public class SimpleCorsResponseFilter implements ContainerResponseFilter {
   @Override
   public ContainerResponse filter(ContainerRequest req, ContainerResponse resp) {
       String requestOrigin = req.getHeaderValue("Origin");
       if (requestOrigin == null) {
           return resp;
       }
       else{
             MultivaluedMap<String, Object> headers = resp.getHttpHeaders();
             headers.add("Access-Control-Allow-Origin", requestOrigin);
             headers.add("Access-Control-Allow-Credentials", "true");
             headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
             headers.add("Access-Control-Allow-Headers", "Content-Type");
             return resp;
       }
   }
}

References (for the wildcard issue):

Thursday, November 6, 2014

AWS SES: Setting Sender Name

This is done by setting the same "source" field used for the email. Instead of using the email address by itself, include the name in this format "The Name <the@email.com>"

Reference:

Enabling CORS with Jersey

CORS is a framework for allowing a webpage from one site to call e.g. REST services on site.

In Java, there are open source frameworks that provides filters to support this easily e.g. http://software.dzhuvinov.com/cors-filter.html

However, for Jersey, it is actually very simple and these Servlet Filter based frameworks can be too overcomplicated / heavyweight for the need.

To enable CORS in Jersey is just a matter of implementing and registering a ContainerResponseFilter with the following:


public class SimpleCorsResponseFilter implements ContainerResponseFilter {
   @Override
   public ContainerResponse filter(ContainerRequest req, ContainerResponse resp) {
       MultivaluedMap<String, Object> headers = resp.getHttpHeaders();
       headers.add("Access-Control-Allow-Origin", "*");
       headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
       headers.add("Access-Control-Allow-Headers", "Content-Type");
       return resp;
   }
}

To register this with Jersey, add the following Servlet init-params in the web.xml


<init-param>
   <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
   <param-value>xxx.yyy.zzz.SimpleCorsResponseFilter</param-value>
</init-param>

Note: code was written for Jersey version 1.17

References: