+ Added Servlet for processing CT creation request
parent
166669c287
commit
678c2874a2
@ -0,0 +1,66 @@
|
|||||||
|
package fr.sixthemes.beans;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class CTCreationRequest {
|
||||||
|
|
||||||
|
protected String targetClusterURL;
|
||||||
|
protected String apiID;
|
||||||
|
protected String apiKey;
|
||||||
|
|
||||||
|
public CTCreationRequest() {
|
||||||
|
// TODO : Get default credentials from user session or docker env
|
||||||
|
this.targetClusterURL = "https://example.com";
|
||||||
|
}
|
||||||
|
|
||||||
|
public CTCreationRequest(String targetClusterURL, String apiID, String apiKey) {
|
||||||
|
this.targetClusterURL = targetClusterURL;
|
||||||
|
this.apiID = apiID;
|
||||||
|
this.apiKey = apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String sendRequest(CTConf ctConfiguration) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
String postData = this.getPostDataFor(ctConfiguration);
|
||||||
|
// TODO : remove hardcoded parameters
|
||||||
|
postData += "&ostemplate=local%3Avztmpl%2Fdebian-11-standard_11.6-1_amd64.tar.zst&vmid=8000";
|
||||||
|
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(this.targetClusterURL + "?" + postData))
|
||||||
|
.header("Authorization", "PVEAPIToken=" + this.apiID + "=" + this.apiKey)
|
||||||
|
.method("POST", HttpRequest.BodyPublishers.noBody())
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
return response.body();
|
||||||
|
}
|
||||||
|
catch(Exception e) {
|
||||||
|
return e.getStackTrace().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPostDataFor(CTConf conf) throws UnsupportedEncodingException {
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
|
for (Map.Entry<String, String> entry : conf.getParameters().entrySet()) {
|
||||||
|
builder.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
|
||||||
|
builder.append("=");
|
||||||
|
builder.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
|
||||||
|
builder.append("&");
|
||||||
|
}
|
||||||
|
|
||||||
|
String resultString = builder.toString();
|
||||||
|
return resultString.length() > 0
|
||||||
|
? resultString.substring(0, resultString.length() - 1)
|
||||||
|
: resultString;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package fr.sixthemes.servlets;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import fr.sixthemes.beans.CTConf;
|
||||||
|
import fr.sixthemes.beans.CTCreationRequest;
|
||||||
|
|
||||||
|
@WebServlet(urlPatterns = "/process")
|
||||||
|
public class RequestProcessor extends HttpServlet {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
CTConf conf = (CTConf) req.getSession().getAttribute("conf");
|
||||||
|
|
||||||
|
String targetUrl = req.getParameter("target");
|
||||||
|
String tokenID = req.getParameter("tokenid");
|
||||||
|
String tokenKey = req.getParameter("tokenkey");
|
||||||
|
|
||||||
|
CTCreationRequest request = new CTCreationRequest(targetUrl, tokenID, tokenKey);
|
||||||
|
|
||||||
|
String response = request.sendRequest(conf);
|
||||||
|
|
||||||
|
PrintWriter out = resp.getWriter();
|
||||||
|
|
||||||
|
out.println(response);
|
||||||
|
// out.println(request.getPostDataFor(conf));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue