BrightSide Workbench Full Report + Source Code
P2PClient.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.alliance.service;
20 
21 import java.io.IOException;
22 import java.net.URLDecoder;
23 import java.nio.charset.StandardCharsets;
24 import java.util.Objects;
25 import javax.servlet.ServletException;
26 import javax.servlet.annotation.WebServlet;
27 import javax.servlet.http.HttpServlet;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import org.turro.collections.KeyValueMap;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantApplication;
33 import org.turro.http.Respond;
34 import org.turro.json.Jsons;
35 import org.turro.ws.WsServer;
36 import org.turro.ws.WsServerPK;
37 import org.turro.ws.service.member.Servers;
38 import org.turro.ws.service.p2p.P2Ps;
39 
44 @WebServlet(name = "P2PClient", urlPatterns = {"/p2pc/*"})
45 public class P2PClient extends HttpServlet {
46 
47  private void processP2P(Application application, String path, KeyValueMap map) throws IOException {
48  if(Objects.equals("/attach", path)) {
49  HttpServletResponse response = application.getHttpServletResponse();
50  WsServer server = Servers.getServer(WsServerPK.from(map.get(String.class, "d"), AxServer.SERVER_SERVICE));
51  Jsons criteria = Jsons.object().add("attachId", map.get(Long.class, "a"));
52  Respond.to(response).name(map.get("n")).flush(
53  P2Ps.getP2PContent(server.getServerDomain(), map.get(Long.class, "m"),
54  AxP2PServer.AX_P2P_SERVICE, AxConstants.ATTACHMENT, criteria));
55  }
56  }
57 
58  protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
59  KeyValueMap map = new KeyValueMap();
60  for(String key : request.getParameterMap().keySet()) {
61  map.put(key, URLDecoder.decode(request.getParameter(key), StandardCharsets.UTF_8));
62  }
63  processP2P(new ElephantApplication(request, response), request.getPathInfo(), map);
64  }
65 
66  @Override
67  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
68  processRequest(request, response);
69  }
70 
71  @Override
72  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
73  processRequest(request, response);
74  }
75 
76  @Override
77  public String getServletInfo() {
78  return "P2P client servlet";
79  }
80 
81 }
static final String SERVER_SERVICE
Definition: AxServer.java:51
void doGet(HttpServletRequest request, HttpServletResponse response)
Definition: P2PClient.java:67
void doPost(HttpServletRequest request, HttpServletResponse response)
Definition: P2PClient.java:72
void processRequest(HttpServletRequest request, HttpServletResponse response)
Definition: P2PClient.java:58
abstract HttpServletResponse getHttpServletResponse()
static WsServerPK from(String serverDomain, String service)
Definition: WsServerPK.java:33
String getServerDomain()
Definition: WsServer.java:52