/* $Id$ */

/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.manifoldcf.rss_tests;

import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

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


import java.io.*;
import java.util.*;

/** Mock wiki service */
public class MockSolrService
{
  Server server;
  SolrServlet servlet;
    
  public MockSolrService()
  {
    server = new Server(8188);
    server.setThreadPool(new QueuedThreadPool(35));
    servlet = new SolrServlet();
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);
    context.addServlet(new ServletHolder(servlet), "/*");
  }
    
  public void start() throws Exception
  {
    server.start();
  }
    
  public void stop() throws Exception
  {
    server.stop();
  }

  
  public static class SolrServlet extends HttpServlet
  {
    public SolrServlet()
    {
    }
    
    @Override
    public void service(HttpServletRequest req, HttpServletResponse res)
      throws IOException
    {
      try
      {
        // Get path part of request URL
        String pathPart = req.getPathInfo();
        if (pathPart == null)
          generateMissingPageResponse(res);
        else if (pathPart.equals("/admin/ping"))
        {
          generatePingResponse(res);
        }
        else if (pathPart.equals("/update/extract"))
        {
          generateUpdateResponse(res);
        }
        else if (pathPart.equals("/update"))
        {
          generateDeleteResponse(res);
        }
        else
          generateMissingPageResponse(res);
      }
      catch (IOException e)
      {
        e.printStackTrace();
        throw e;
      }

    }

    protected static void generatePingResponse(HttpServletResponse res)
      throws IOException
    {
      res.setStatus(HttpServletResponse.SC_OK);
      res.setContentType("text/xml; charset=utf-8");
      res.getWriter().printf("<solr>\n");
      res.getWriter().printf("</solr>\n");
    }
    
    protected static void generateUpdateResponse(HttpServletResponse res)
      throws IOException
    {
      res.setStatus(HttpServletResponse.SC_OK);
      res.setContentType("text/xml; charset=utf-8");
      res.getWriter().printf("<result>\n");
      res.getWriter().printf("  <doc name=\"something\"/>\n");
      res.getWriter().printf("</result>\n");
    }
    
    protected static void generateDeleteResponse(HttpServletResponse res)
      throws IOException
    {
      res.setStatus(HttpServletResponse.SC_OK);
      res.setContentType("text/xml; charset=utf-8");
      res.getWriter().printf("<result>\n");
      res.getWriter().printf("  <doc name=\"something\"/>\n");
      res.getWriter().printf("</result>\n");
    }
    
    protected static void generateMissingPageResponse(HttpServletResponse res)
      throws IOException
    {
      res.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
    
    protected static void generateBadArgumentResponse(HttpServletResponse res)
      throws IOException
    {
      res.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }

  }

}

Add a mock solr server, so we can write a test for the IOException issues