package org.intermine.webservice.server;

/*
 * Copyright (C) 2002-2011 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.intermine.util.StringUtil;
import org.intermine.web.logic.Constants;
import org.intermine.web.logic.session.SessionMethods;

/**
 * Returns version of the deployed InterMine application.
 *
 * @author Jakub Kulaviak
 */
public class VersionServlet extends HttpServlet
{

    private static final Logger LOGGER = Logger.getLogger(VersionServlet.class);

    private static final long serialVersionUID = 1L;

    /**
     * {@inheritDoc}}
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        runService(request, response);
    }

    /**
     * {@inheritDoc}}
     */
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        runService(req, resp);
    }

    private void runService(HttpServletRequest request,
            HttpServletResponse response) {
        String pathFromUrl = request.getPathInfo();
        String version = getVersion(pathFromUrl, request);
        try {
            response.getWriter().print(version);
        } catch (IOException e) {
            LOGGER.error("Obtaining writer to write intermine version failed.", e);
        }
    }

    private String getVersion(String versionType, HttpServletRequest request) {
        if (versionType != null) {
            versionType = StringUtil.trimSlashes(versionType);

            if (versionType.equalsIgnoreCase("release")) {
                Properties webProperties =
                    SessionMethods.getWebProperties(request.getSession().getServletContext());
                return webProperties.getProperty("project.releaseVersion");
            } else if (versionType.equalsIgnoreCase("ws")) {
                return "" + Constants.WEB_SERVICE_VERSION;
            }
        }
        // for backwards compatibility default is the web service version
        return "" + Constants.WEB_SERVICE_VERSION;
    }
}

@@ -364,37 +364,8 @@
 ion;
-%0Aimport java.util.Properties;
 %0A%0Aim
@@ -549,107 +549,34 @@
 org.
-apache.log4j.Logger;%0Aimport org.intermine.util.StringUtil;%0Aimport org.i
+intermine.api.I
 nter
-mine.web.logic.Constants
+MineAPI
 ;%0Aim
@@ -693,16 +693,40 @@
 tion.%0A *
+ @author Alex Kalderimis
 %0A * @aut
@@ -802,91 +802,8 @@
 t%0A%7B%0A
-%0A    private static final Logger LOGGER = Logger.getLogger(VersionServlet.class);%0A%0A
     
@@ -1314,28 +1314,16 @@
 request,
-%0A           
  HttpSer
@@ -1359,386 +1359,72 @@
     
-String pathFromUrl = request.getPathInfo();%0A        String version = getVersion(pathFromUrl, request);%0A        try %7B%0A            response.getWriter().print(version);%0A        %7D catch (IOException e) %7B%0A            LOGGER.error(%22Obtaining writer to write intermine version failed.%22, e);%0A        %7D%0A    %7D%0A%0A    private String getVersion(String versionType, HttpServletRequest 
+final InterMineAPI api = SessionMethods.getInterMineAPI(
 request)
  %7B%0A 
@@ -1419,18 +1419,17 @@
 request)
- %7B
+;
 %0A       
@@ -1433,639 +1433,58 @@
     
-if (versionType != null) %7B%0A            versionType = StringUtil.trimSlashes(versionType);%0A%0A            if (versionType.equalsIgnoreCase(%22release%22)) %7B%0A                Properties webProperties =%0A                    SessionMethods.getWebProperties(request.getSession().getServletContext());%0A                return webProperties.getProperty(%22project.releaseVersion%22);%0A            %7D else if (versionType.equalsIgnoreCase(%22ws%22)) %7B%0A                return %22%22 + Constants.WEB_SERVICE_VERSION;%0A            %7D%0A        %7D%0A        // for backwards compatibility default is the web service version%0A        return %22%22 + Constants.WEB_SERVICE_VERSION
+new VersionService(api).service(request, response)
 ;%0A  

Move handler logic into webservice