Sunday, November 20, 2011

Advantages of working remote

There are many disadvantages of working remote  but there are many advantages of working remote.
  1. You are saving time on commute so you get more time for family if you are disciplined enough to start/stop work at office times (rarely happens on all days but still better than getting stuck in traffic)
  2. Less people coming and distracting you by just walking into cubicle to discuss office politics
  3. Less people coming and asking you things that they can just google for themeselves.
  4. Due to the above points you get more work done in less amount of time.
  5. You RTFM more than usual. If you are stuck on an issue you are the only one who can find the solution as the luxary of walking into someone's cubicle for help is gone, In a sense its a double edged sword but as you are left with no choice in the end you come up as winner and you become more and more of problem solver on your own. This way you tend to research things in detail and increase your arsenal of skills.

Tuesday, November 15, 2011

openoffice and NFS file saving issue

I recently integrated a file preview application in our application with team, so now users can preview most of the files without downloading them. The hard part was to deal with NFS issues due to locking and caching. We chose to buy v/s build for the file preview and bought some third party service. The third party service lets call it APreview had a http api where you can pass the source and target file path. It would have been much better if we could stream the input and it could stream the output but that option was not there. Because we can only pass paths to it the natural solution was to use NFS paths. So we ran into two major issues:

1)The APreview application internally uses openoffice to convert word/PPT/Xls files to pdf and then it converts pdf to swf. openoffice has some issues with writing to NFS and we could use vi and other tools to write files but openoffice would just refuse to save the file as pdf. Finally I found that commenting these two lines in /usr/lib64/openoffice.org3/program/soffice made the thing working
#SAL_ENABLE_FILE_LOCKING=1
#export SAL_ENABLE_FILE_LOCKING

you have to comment it out. setting this SAL_ENABLE_FILE_LOCKING=0 wont work and dont forget to comment out the 'export' also.

2) Serving file over NFS had issues because of NFS cache lag. The VM where APreview was installed would write the file on NFS mount but the tomcat that has to serve the file was seeing the file after 15 sec delay. We tried all sort of things and ultimately gave up on NFS to serve the file. We installed apache on the Filer-server where preview were stored and served the file over http using apache reverse proxy.

Disadvantages of working remote

Just ranting it out loud, I am working remote for the past two years for a startup and there are many advantages of working remote but there are many disadvantages also.

  • Its hard to chase people over phone. As people wont pick up the call or they wont reply to Instant Messages. So you are stuck in your chain of thoughts cursing the monitor.
  • You miss the coffee talk.
  • You miss what's going on in the office and really dont know whats going in the company.
  • In meetings you could be writing code and its easy to get distracted over phone.
  • Some people prefer to chat over talk and thats a pain because you already miss the social connection and typing is always a pain.
  • You miss all company dinners and lunches.
  • Because you save time on travel you tend to overwork.
  • Worse if you are working in a different timezone then people would disturb you off hours.
  • Lots of time is spent in trying to screen share or do go to meeting.
  • People will prefer to talk to person in house and unless you are a rockstar and only you can deliver the feature mostly people will prefer in house person.
  • Lots of things will get discussed in adhoc passageway meeting and you would miss all of that.
  • Dealing with HR is a pain. As you are remote you dont have the luxary of walking into their cube and getting your work done. I am literally screwed on every work that needs to flow through HR. Communicating with them on email leads to frustration and all you can do is curse your monitor.
Also check my post on Advantages of working remote

Friday, November 4, 2011

Jesey writing an authentication filter

It seems there are two ways to add authentication to Jersey REST apis

1) You can add a servlet filter.
public class RestAuthenticationFilter implements Filter {
    @Override
    public void destroy() {
        // TODO Auto-generated method stub        
    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {     
    try {
           User user = BasicAuthHelper.authenticateUser(request);
            if (user == null) {
                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
            } else {
                request.setAttribute("user", user);
                chain.doFilter(request, response);
            }
     } catch (ApplicationException e) {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
     }
    }
    @Override
    public void init(FilterConfig config) throws ServletException {
    } 
} 


2) You can do it using the jersey filter. You have to implement a ResourceFilterFactory and handle the auth in ContainerRequestFilter. The detailed code is below.  I like the approach 1 as it give complete lifecycle control. However if you need more specifc things like accessing QueryParams or PathParams then approach 2 is the way to go


public class RestAuthFilterFactory implements ResourceFilterFactory {
    private static final AppLogger logger = AppLogger
            .getLogger(RestAuthFilterFactory.class);

    @Context
    private UriInfo uriInfo;

    @Override
    public List create(AbstractMethod method) {
        return Collections.singletonList((ResourceFilter) new Filter());
    }

    private class Filter implements ResourceFilter, ContainerRequestFilter {
        protected Filter() {
        }

        public ContainerRequestFilter getRequestFilter() {
            return this;
        }

        public ContainerResponseFilter getResponseFilter() {
            return null;
        }

        public ContainerRequest filter(ContainerRequest request) {
            logger.info("Url invoked is {}", uriInfo.getPath());
            String authHeader = request.getHeaderValue("Authorization");
            if (authHeader != null && authHeader.startsWith("Basic ")) {
                   User user = BasicAuthHelper.authenticateUser(request);
               if (user == null) {
                  throw new WebApplicationException(Response.Status.UNAUTHORIZED);
               }
            return request;
            }
              throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }
        }
}