root/SPARQLBuilderWWW2016/src/java/org/biohackathon/SPARQLBuilder/www/EPServlet.java @ 277

リビジョン 267, 4.3 KB (コミッタ: atsuko, 9 年 前)

メタデータ仕様変更,フェデレートサーチ対応向け

行番号 
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6
7package org.biohackathon.SPARQLBuilder.www;
8
9import java.io.IOException;
10import java.io.PrintWriter;
11import javax.servlet.ServletException;
12import javax.servlet.annotation.WebServlet;
13import javax.servlet.http.HttpServlet;
14import javax.servlet.http.HttpServletRequest;
15import javax.servlet.http.HttpServletResponse;
16import javax.servlet.http.HttpSession;
17import org.biohackathon.SPARQLBuilder.OWL.*;
18import javax.json.*;
19import java.util.*;
20
21//import java.io.*;
22
23/**
24 *
25 * @author atsuko
26 */
27@WebServlet(name = "EPServlet", urlPatterns = {"/eplist"})
28public class EPServlet extends HttpServlet {
29
30    private static final String FILENAME = "cdata/";
31   
32    /**
33     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
34     * methods.
35     *
36     * @param request servlet request
37     * @param response servlet response
38     * @throws ServletException if a servlet-specific error occurs
39     * @throws IOException if an I/O error occurs
40     */
41    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
42            throws ServletException, IOException {
43        response.setContentType("text/html;charset=UTF-8");
44        try (PrintWriter out = response.getWriter()) {
45            /* TODO output your page here. You may use following sample code. */
46            out.println("<!DOCTYPE html>");
47            out.println("<html>");
48            out.println("<head>");
49            out.println("<title>Servlet EPServlet</title>");           
50            out.println("</head>");
51            out.println("<body>");
52            out.println("<h1>Servlet EPServlet at " + request.getContextPath() + "</h1>");
53            out.println("</body>");
54            out.println("</html>");
55        }
56    }
57
58    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
59    /**
60     * Handles the HTTP <code>GET</code> method.
61     *
62     * @param request servlet request
63     * @param response servlet response
64     * @throws ServletException if a servlet-specific error occurs
65     * @throws IOException if an I/O error occurs
66     */
67    @Override
68    protected void doGet(HttpServletRequest request, HttpServletResponse response)
69            throws ServletException, IOException {
70        HttpSession session = request.getSession();
71        // Initialize the session
72        session.removeAttribute("qpg");
73        //session.removeAttribute("graph");
74        QueryPathGenerator qpg = new QueryPathGenerator();
75
76        response.setContentType("application/json;charset=UTF-8");
77        response.setHeader("Access-Control-Allow-Origin", "*");
78        response.setHeader("Access-Control-Allow-Methods", "GET");
79        response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));
80        response.setHeader("Access-Control-Max-Age", "-1");       
81        PrintWriter out = response.getWriter();
82
83        String[] elist = qpg.getFactory().getEndpointURIList();
84        SortedSet<String> sortedelist = new TreeSet<String>();
85        for (int i = 0; i < elist.length; i++ ){
86            sortedelist.add(elist[i]);
87        }       
88        JsonBuilderFactory jbfactory = Json.createBuilderFactory(null);
89        JsonArrayBuilder jab = jbfactory.createArrayBuilder();
90        Iterator<String> eit = sortedelist.iterator();
91        while( eit.hasNext() ){
92            jab.add(eit.next());           
93        }
94        JsonArray ja = jab.build();
95        out.print(ja);
96        session.setAttribute("qpg", qpg);
97    }
98
99    /**
100     * Handles the HTTP <code>POST</code> method.
101     *
102     * @param request servlet request
103     * @param response servlet response
104     * @throws ServletException if a servlet-specific error occurs
105     * @throws IOException if an I/O error occurs
106     */
107    @Override
108    protected void doPost(HttpServletRequest request, HttpServletResponse response)
109            throws ServletException, IOException {
110        processRequest(request, response);
111    }
112
113    /**
114     * Returns a short description of the servlet.
115     *
116     * @return a String containing servlet description
117     */
118    @Override
119    public String getServletInfo() {
120        return "Short description";
121    }// </editor-fold>
122
123}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。