2012-06-14 20 views
0

我有一個用於存儲和檢索桌面客戶端文件的JAX-RS REST Web應用程序。我將在兩個不同的服務器上的兩個不同的環境中部署它,所以我希望將文件存儲的路徑配置在代碼之外。從REST資源訪問Servlet初始化參數

我知道如何從Servlet讀取初始化參數(在web.xml中)。我可以爲REST資源類做些類似的事嗎?如果我可以從WEB-INF目錄內的其他文件中讀取,那麼它應該也可以正常工作。

這是我的工作代碼:

import javax.ws.rs.*; 
import java.io.*; 

@Path("/upload") 
public class UploadSchedule { 

    static String path = "/home/proctor/data/schoolData/"; 
    //I would like to store the path value in web.xml 
    @PUT 
    @Path("/pxml/{id}/") 
    @Consumes("text/xml") @Produces("text/plain") 
    public String receiveSchedule(@PathParam("id") final Integer schoolID, String content) { 
     if (saveFile(schoolID, "schedule.pxml", content)) 
      return schoolID + " saved assignment schedule." 
     else 
      return "Error writing schedule. ("+content.length()+" Bytes)"; 
    } 

    /** 
    * Receives and stores the CSV file faculty list. The location on the server 
    * is not directly associated with the request URI. 
    * @param schoolID 
    * @param content 
    * @return a String confirmation message. 
    */ 
    @POST 
    @Path("/faculty/{id}/") 
    @Consumes("text/plain")  @Produces("text/plain") 
    public String receiveFaculty(@PathParam("id") final Integer schoolID, String content) { 
     if (saveFile(schoolID, "faculty.csv", content)) 
      return schoolID + " saved faculty."; 
     else 
      return "Error writing faculty file.(" +content.length()+ " Bytes)"; 

    } 
    //more methods like these 

    /** 
    * Saves content sent from the user to the specified filename. 
    * The directory is determined by the static field in this class and 
    * by the school id. 
    * @param id SchoolID 
    * @param filename location to save content 
    */ 
    private boolean saveFile(int id, String filename, String content) { 
     File saveDirectory = (new File(path + id)); 
     if (!saveDirectory.exists()) { 
      //create the directory since it isn't there yet. 
      if (!saveDirectory.mkdir()) 
       return false; 
     } 
     File saveFile = new File(saveDirectory, filename); 
     try(FileWriter writer = new FileWriter(saveFile)) { 
      writer.write(content); 
      return true; 
     } catch (IOException ioe) { 
      return false; 
     } 
    } 
} 

回答

0

首先,你必須得使用

@Context 
ServletContext context; 

然後你的休息資源

context.getInitParameter("praram-name") 
+0

唯一的問題是,我後來才發現,它沒有工作。 – Thorn

+0

這不起作用。這種情況下的上下文是應用外觀 – gshauger

3

雖然裏面的servlet上下文從web.xml獲得初始化參數看起來是一個常見的任務,我花了相當一段時間才弄清楚了這一點並找到一個有效的解決方案。爲了避免我的挫折,讓我發佈我的解決方案。我正在使用Jersey實現,即 com.sun.jersey.spi.container.servlet.ServletContainer也許其他REST實現可以使用ServletContext訪問web.xml初始參數,但儘管文檔引導我相信這會起作用,但它沒有。

我需要使用以下代替:@Context ResourceConfig context; 這被列爲我的資源類中的一個字段。然後我的資源的方法之一內,我可以用下面的訪問web.xml中初始化參數:

String uploadDirectory = (String) context.getProperty("dataStoragePath"); 

凡該財產是指在web.xml文件:

<init-param> 
     <param-name>dataStoragePath</param-name> 
     <param-value>C:/ztestServer</param-value> 
    </init-param> 

出人意料的是,當我使用@Context ServletContext context;時,我發現上下文對象確實引用了ApplicationContextFacade。我看不到通過該Facade並訪問我所關心的信息的方法。我打印出來的參數圖,它顯示我的唯一參數,這個對象是意識到:

java.util.Enumeration<String> params = context.getInitParameterNames(); 
    while(params.hasMoreElements()) 
     System.out.println(params.nextElement()); 

輸出:

com.sun.faces.forceLoadConfiguration 
com.sun.faces.validateXml