javamanager/javainstaller/installer/tsrc/testserver/javasrc/com/nokia/mj/impl/installer/testserver/JadServlet.java
branchRCL_3
changeset 83 26b2b12093af
parent 77 7cee158cb8cd
child 84 0553e2305d00
equal deleted inserted replaced
77:7cee158cb8cd 83:26b2b12093af
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.mj.impl.installer.testserver;
       
    19 
       
    20 import java.io.BufferedReader;
       
    21 import java.io.FileReader;
       
    22 import java.io.FileNotFoundException;
       
    23 import java.io.IOException;
       
    24 
       
    25 import javax.servlet.ServletContext;
       
    26 import javax.servlet.ServletException;
       
    27 import javax.servlet.ServletOutputStream;
       
    28 import javax.servlet.http.HttpServlet;
       
    29 import javax.servlet.http.HttpServletRequest;
       
    30 import javax.servlet.http.HttpServletResponse;
       
    31 
       
    32 /**
       
    33  * Servlet implementation class JadServlet
       
    34  */
       
    35 public class JadServlet extends HttpServlet
       
    36 {
       
    37     private static final long serialVersionUID = 1L;
       
    38 
       
    39     protected void processRequest(HttpServletRequest request,
       
    40                                   HttpServletResponse response)
       
    41     throws ServletException, IOException
       
    42     {
       
    43         ServletContext context = getServletContext();
       
    44         String localPath = context.getRealPath(request.getServletPath());
       
    45 
       
    46         response.setContentType("text/vnd.sun.j2me.app-descriptor");
       
    47 
       
    48         BufferedReader in = null;
       
    49         ServletOutputStream out = null;
       
    50 
       
    51         try
       
    52         {
       
    53             in = new BufferedReader(new FileReader(localPath));
       
    54             out = response.getOutputStream();
       
    55 
       
    56             String line = in.readLine();
       
    57             while (line != null)
       
    58             {
       
    59                 if (line.startsWith("MIDlet-Jar-URL:")
       
    60                         || line.startsWith("MIDlet-Install-Notify:")
       
    61                         || line.startsWith("MIDlet-Delete-Notify:"))
       
    62                 {
       
    63                     line = line.replaceAll("<SERVER>", request.getServerName()
       
    64                                            + ":" + request.getServerPort());
       
    65                     line = line.replaceAll("<CONTEXT>",
       
    66                                            request.getContextPath());
       
    67                 }
       
    68                 out.println(line);
       
    69                 line = in.readLine();
       
    70             }
       
    71         }
       
    72         catch (FileNotFoundException e)
       
    73         {
       
    74             response.sendError(HttpServletResponse.SC_NOT_FOUND,
       
    75                                request.getServletPath());
       
    76         }
       
    77         finally
       
    78         {
       
    79             if (out != null)
       
    80             {
       
    81                 out.close();
       
    82             }
       
    83             if (in != null)
       
    84             {
       
    85                 in.close();
       
    86             }
       
    87         }
       
    88     }
       
    89 
       
    90     protected void doGet(HttpServletRequest request,
       
    91                          HttpServletResponse response)
       
    92     throws ServletException, IOException
       
    93     {
       
    94         processRequest(request, response);
       
    95     }
       
    96 
       
    97     protected void doPost(HttpServletRequest request,
       
    98                           HttpServletResponse response)
       
    99     throws ServletException, IOException
       
   100     {
       
   101         processRequest(request, response);
       
   102     }
       
   103 
       
   104 }