Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7764b25
commit 9dc0d5d
Showing
19 changed files
with
577 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
18.0.8 | ||
18.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
src/main/java/com/mxgraph/online/ExportProxyServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package com.mxgraph.online; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.Arrays; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* Servlet implementation ExportProxyServlet | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class ExportProxyServlet extends HttpServlet | ||
{ | ||
private final String[] supportedServices = {"EXPORT_URL", "PLANTUML_URL", "VSD_CONVERT_URL", "EMF_CONVERT_URL"}; | ||
|
||
private void doRequest(String method, HttpServletRequest request, | ||
HttpServletResponse response) throws ServletException, IOException | ||
{ | ||
try | ||
{ | ||
int serviceId = 0; | ||
String proxyPath = ""; | ||
String queryString = ""; | ||
|
||
try | ||
{ | ||
if (request.getQueryString() != null) | ||
{ | ||
queryString = "?" + request.getQueryString(); | ||
} | ||
|
||
if (request.getPathInfo() != null) // /{serviceId}/* | ||
{ | ||
String[] pathParts = request.getPathInfo().split("/"); | ||
|
||
if (pathParts.length > 1) | ||
{ | ||
serviceId = Integer.parseInt(pathParts[1]); | ||
} | ||
|
||
if (pathParts.length > 2) | ||
{ | ||
proxyPath = String.join("/", Arrays.copyOfRange(pathParts, 2, pathParts.length)); | ||
} | ||
|
||
if (serviceId < 0 || serviceId > supportedServices.length) | ||
{ | ||
serviceId = 0; | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
// Ignore and use 0 | ||
serviceId = 0; | ||
} | ||
|
||
String exportUrl = System.getenv(supportedServices[serviceId]); | ||
|
||
if (exportUrl == null || exportUrl.isEmpty()) | ||
{ | ||
throw new Exception(supportedServices[serviceId] + " not set"); | ||
} | ||
|
||
URL url = new URL(exportUrl + proxyPath + queryString); | ||
HttpURLConnection con = (HttpURLConnection) url.openConnection(); | ||
|
||
con.setRequestMethod(method); | ||
|
||
//Copy request headers to export server | ||
Enumeration<String> headerNames = request.getHeaderNames(); | ||
|
||
while (headerNames.hasMoreElements()) | ||
{ | ||
String headerName = headerNames.nextElement(); | ||
Enumeration<String> headers = request.getHeaders(headerName); | ||
|
||
while (headers.hasMoreElements()) | ||
{ | ||
String headerValue = headers.nextElement(); | ||
con.addRequestProperty(headerName, headerValue); | ||
} | ||
} | ||
|
||
if ("POST".equals(method)) | ||
{ | ||
// Send post request | ||
con.setDoOutput(true); | ||
|
||
OutputStream params = con.getOutputStream(); | ||
Utils.copy(request.getInputStream(), params); | ||
params.flush(); | ||
params.close(); | ||
} | ||
|
||
int responseCode = con.getResponseCode(); | ||
//Copy response code | ||
response.setStatus(responseCode); | ||
|
||
//Copy response headers | ||
Map<String, List<String>> map = con.getHeaderFields(); | ||
|
||
for (Map.Entry<String, List<String>> entry : map.entrySet()) | ||
{ | ||
String key = entry.getKey(); | ||
|
||
if (key != null) | ||
{ | ||
for (String val : entry.getValue()) | ||
{ | ||
|
||
response.addHeader(entry.getKey(), val); | ||
} | ||
} | ||
} | ||
|
||
//Copy response | ||
OutputStream out = response.getOutputStream(); | ||
|
||
//Error | ||
if (responseCode >= 400) | ||
{ | ||
Utils.copy(con.getErrorStream(), out); | ||
} | ||
else //Success | ||
{ | ||
Utils.copy(con.getInputStream(), out); | ||
} | ||
|
||
out.flush(); | ||
out.close(); | ||
} | ||
catch (Exception e) | ||
{ | ||
response.setStatus( | ||
HttpServletResponse.SC_INTERNAL_SERVER_ERROR); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) | ||
*/ | ||
protected void doGet(HttpServletRequest request, | ||
HttpServletResponse response) throws ServletException, IOException | ||
{ | ||
doRequest("GET", request, response); | ||
} | ||
|
||
/** | ||
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) | ||
*/ | ||
protected void doPost(HttpServletRequest request, | ||
HttpServletResponse response) throws ServletException, IOException | ||
{ | ||
doRequest("POST", request, response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.