1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2025-02-03 19:18:25 +00:00

HTTP redirect after POST

This commit is contained in:
Arnaud Roques 2016-05-18 22:49:58 +02:00
parent 94fb4cd383
commit 85699130d0

View File

@ -58,132 +58,132 @@ import net.sourceforge.plantuml.png.MetadataTag;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class PlantUmlServlet extends HttpServlet { public class PlantUmlServlet extends HttpServlet {
private static final String DEFAULT_ENCODED_TEXT = "SyfFKj2rKt3CoKnELR1Io4ZDoSa70000"; private static final String DEFAULT_ENCODED_TEXT = "SyfFKj2rKt3CoKnELR1Io4ZDoSa70000";
// Last part of the URL // Last part of the URL
public static final Pattern urlPattern = Pattern.compile("^.*[^a-zA-Z0-9\\-\\_]([a-zA-Z0-9\\-\\_]+)"); public static final Pattern urlPattern = Pattern.compile("^.*[^a-zA-Z0-9\\-\\_]([a-zA-Z0-9\\-\\_]+)");
// Format of a compressed diagram // Format of a compressed diagram
public static final Pattern encodedPattern = Pattern.compile("^[a-zA-Z0-9\\-\\_]+$"); public static final Pattern encodedPattern = Pattern.compile("^[a-zA-Z0-9\\-\\_]+$");
private static final Pattern recoverUmlPattern = Pattern.compile("/\\w+/uml/(.*)"); private static final Pattern recoverUmlPattern = Pattern.compile("/\\w+/uml/(.*)");
@Override @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8");
String text = request.getParameter("text"); String text = request.getParameter("text");
String metadata = request.getParameter("metadata"); String metadata = request.getParameter("metadata");
if (metadata != null) { if (metadata != null) {
InputStream img = null; InputStream img = null;
try { try {
img = getImage(new URL(metadata)); img = getImage(new URL(metadata));
MetadataTag metadataTag = new MetadataTag(img, "plantuml"); MetadataTag metadataTag = new MetadataTag(img, "plantuml");
String data = metadataTag.getData(); String data = metadataTag.getData();
if (data != null) { if (data != null) {
text = data; text = data;
} }
} finally { } finally {
if (img != null) { if (img != null) {
img.close(); img.close();
} }
} }
} }
try { try {
text = getTextFromUrl(request, text); text = getTextFromUrl(request, text);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
// no Text form has been submitted // no Text form has been submitted
if (text == null || text.trim().isEmpty()) { if (text == null || text.trim().isEmpty()) {
redirectNow(request, response, DEFAULT_ENCODED_TEXT); redirectNow(request, response, DEFAULT_ENCODED_TEXT);
return; return;
} }
final String encoded = getTranscoder().encode(text); final String encoded = getTranscoder().encode(text);
request.setAttribute("decoded", text); request.setAttribute("decoded", text);
request.setAttribute("encoded", encoded); request.setAttribute("encoded", encoded);
// check if an image map is necessary // check if an image map is necessary
if (text != null && PlantumlUtils.hasCMapData(text)) { if (text != null && PlantumlUtils.hasCMapData(text)) {
request.setAttribute("mapneeded", Boolean.TRUE); request.setAttribute("mapneeded", Boolean.TRUE);
} }
// forward to index.jsp // forward to index.jsp
final RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp"); final RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response); dispatcher.forward(request, response);
} }
@Override @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException { IOException {
request.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8");
String text = request.getParameter("text"); String text = request.getParameter("text");
String encoded = DEFAULT_ENCODED_TEXT; String encoded = DEFAULT_ENCODED_TEXT;
try { try {
text = getTextFromUrl(request, text); text = getTextFromUrl(request, text);
encoded = getTranscoder().encode(text); encoded = getTranscoder().encode(text);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
redirectNow(request, response, encoded); redirectNow(request, response, encoded);
} }
private String getTextFromUrl(HttpServletRequest request, String text) throws IOException { private String getTextFromUrl(HttpServletRequest request, String text) throws IOException {
String url = request.getParameter("url"); String url = request.getParameter("url");
final Matcher recoverUml = recoverUmlPattern.matcher(request.getRequestURI()); final Matcher recoverUml = recoverUmlPattern.matcher(request.getRequestURI());
// the URL form has been submitted // the URL form has been submitted
if (recoverUml.matches()) { if (recoverUml.matches()) {
final String data = recoverUml.group(1); final String data = recoverUml.group(1);
text = getTranscoder().decode(data); text = getTranscoder().decode(data);
} else if (url != null && !url.trim().isEmpty()) { } else if (url != null && !url.trim().isEmpty()) {
// Catch the last part of the URL if necessary // Catch the last part of the URL if necessary
final Matcher m1 = urlPattern.matcher(url); final Matcher m1 = urlPattern.matcher(url);
if (m1.find()) { if (m1.find()) {
url = m1.group(1); url = m1.group(1);
} }
text = getTranscoder().decode(url); text = getTranscoder().decode(url);
} }
return text; return text;
} }
private void redirectNow(HttpServletRequest request, HttpServletResponse response, String encoded) private void redirectNow(HttpServletRequest request, HttpServletResponse response, String encoded)
throws IOException { throws IOException {
final String result = request.getContextPath() + "/uml/" + encoded; final String result = request.getContextPath() + "/uml/" + encoded;
response.sendRedirect(result); response.sendRedirect(result);
} }
private Transcoder getTranscoder() { private Transcoder getTranscoder() {
return TranscoderUtil.getDefaultTranscoder(); return TranscoderUtil.getDefaultTranscoder();
} }
static private HttpURLConnection getConnection(URL url) throws IOException { static private HttpURLConnection getConnection(URL url) throws IOException {
if (url.getProtocol().startsWith("https")) { if (url.getProtocol().startsWith("https")) {
HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET"); con.setRequestMethod("GET");
con.setReadTimeout(10000); // 10 seconds con.setReadTimeout(10000); // 10 seconds
// printHttpsCert(con); // printHttpsCert(con);
con.connect(); con.connect();
return con; return con;
} else { } else {
HttpURLConnection con = (HttpURLConnection) url.openConnection(); HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET"); con.setRequestMethod("GET");
con.setReadTimeout(10000); // 10 seconds con.setReadTimeout(10000); // 10 seconds
con.connect(); con.connect();
return con; return con;
} }
} }
static public InputStream getImage(URL url) throws IOException { static public InputStream getImage(URL url) throws IOException {
InputStream is = null; InputStream is = null;
HttpURLConnection con = getConnection(url); HttpURLConnection con = getConnection(url);
is = con.getInputStream(); is = con.getInputStream();
return is; return is;
} }