Skip to content

Commit

Permalink
HTTP redirect after POST
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudroques committed May 18, 2016
1 parent 94fb4cd commit 8569913
Showing 1 changed file with 126 additions and 126 deletions.
252 changes: 126 additions & 126 deletions src/main/java/net/sourceforge/plantuml/servlet/PlantUmlServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,132 +58,132 @@
@SuppressWarnings("serial")
public class PlantUmlServlet extends HttpServlet {

private static final String DEFAULT_ENCODED_TEXT = "SyfFKj2rKt3CoKnELR1Io4ZDoSa70000";

// Last part of the URL
public static final Pattern urlPattern = Pattern.compile("^.*[^a-zA-Z0-9\\-\\_]([a-zA-Z0-9\\-\\_]+)");

// Format of a compressed diagram
public static final Pattern encodedPattern = Pattern.compile("^[a-zA-Z0-9\\-\\_]+$");

private static final Pattern recoverUmlPattern = Pattern.compile("/\\w+/uml/(.*)");

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
String text = request.getParameter("text");

String metadata = request.getParameter("metadata");
if (metadata != null) {
InputStream img = null;
try {
img = getImage(new URL(metadata));
MetadataTag metadataTag = new MetadataTag(img, "plantuml");
String data = metadataTag.getData();
if (data != null) {
text = data;
}
} finally {
if (img != null) {
img.close();
}
}
}
try {
text = getTextFromUrl(request, text);
} catch (Exception e) {
e.printStackTrace();
}

// no Text form has been submitted
if (text == null || text.trim().isEmpty()) {
redirectNow(request, response, DEFAULT_ENCODED_TEXT);
return;
}

final String encoded = getTranscoder().encode(text);
request.setAttribute("decoded", text);
request.setAttribute("encoded", encoded);

// check if an image map is necessary
if (text != null && PlantumlUtils.hasCMapData(text)) {
request.setAttribute("mapneeded", Boolean.TRUE);
}
// forward to index.jsp
final RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);

}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
request.setCharacterEncoding("UTF-8");

String text = request.getParameter("text");
String encoded = DEFAULT_ENCODED_TEXT;

try {
text = getTextFromUrl(request, text);
encoded = getTranscoder().encode(text);
} catch (Exception e) {
e.printStackTrace();
}

redirectNow(request, response, encoded);
}

private String getTextFromUrl(HttpServletRequest request, String text) throws IOException {
String url = request.getParameter("url");
final Matcher recoverUml = recoverUmlPattern.matcher(request.getRequestURI());
// the URL form has been submitted
if (recoverUml.matches()) {
final String data = recoverUml.group(1);
text = getTranscoder().decode(data);
} else if (url != null && !url.trim().isEmpty()) {
// Catch the last part of the URL if necessary
final Matcher m1 = urlPattern.matcher(url);
if (m1.find()) {
url = m1.group(1);
}
text = getTranscoder().decode(url);
}
return text;
}

private void redirectNow(HttpServletRequest request, HttpServletResponse response, String encoded)
throws IOException {
final String result = request.getContextPath() + "/uml/" + encoded;
response.sendRedirect(result);
}

private Transcoder getTranscoder() {
return TranscoderUtil.getDefaultTranscoder();
}
static private HttpURLConnection getConnection(URL url) throws IOException {
if (url.getProtocol().startsWith("https")) {
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setReadTimeout(10000); // 10 seconds
// printHttpsCert(con);
con.connect();
return con;
} else {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setReadTimeout(10000); // 10 seconds
con.connect();
return con;
}
}

static public InputStream getImage(URL url) throws IOException {
InputStream is = null;
HttpURLConnection con = getConnection(url);
is = con.getInputStream();
return is;
}
private static final String DEFAULT_ENCODED_TEXT = "SyfFKj2rKt3CoKnELR1Io4ZDoSa70000";

// Last part of the URL
public static final Pattern urlPattern = Pattern.compile("^.*[^a-zA-Z0-9\\-\\_]([a-zA-Z0-9\\-\\_]+)");

// Format of a compressed diagram
public static final Pattern encodedPattern = Pattern.compile("^[a-zA-Z0-9\\-\\_]+$");

private static final Pattern recoverUmlPattern = Pattern.compile("/\\w+/uml/(.*)");

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
String text = request.getParameter("text");

String metadata = request.getParameter("metadata");
if (metadata != null) {
InputStream img = null;
try {
img = getImage(new URL(metadata));
MetadataTag metadataTag = new MetadataTag(img, "plantuml");
String data = metadataTag.getData();
if (data != null) {
text = data;
}
} finally {
if (img != null) {
img.close();
}
}
}
try {
text = getTextFromUrl(request, text);
} catch (Exception e) {
e.printStackTrace();
}

// no Text form has been submitted
if (text == null || text.trim().isEmpty()) {
redirectNow(request, response, DEFAULT_ENCODED_TEXT);
return;
}

final String encoded = getTranscoder().encode(text);
request.setAttribute("decoded", text);
request.setAttribute("encoded", encoded);

// check if an image map is necessary
if (text != null && PlantumlUtils.hasCMapData(text)) {
request.setAttribute("mapneeded", Boolean.TRUE);
}
// forward to index.jsp
final RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);

}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
request.setCharacterEncoding("UTF-8");

String text = request.getParameter("text");
String encoded = DEFAULT_ENCODED_TEXT;

try {
text = getTextFromUrl(request, text);
encoded = getTranscoder().encode(text);
} catch (Exception e) {
e.printStackTrace();
}

redirectNow(request, response, encoded);
}

private String getTextFromUrl(HttpServletRequest request, String text) throws IOException {
String url = request.getParameter("url");
final Matcher recoverUml = recoverUmlPattern.matcher(request.getRequestURI());
// the URL form has been submitted
if (recoverUml.matches()) {
final String data = recoverUml.group(1);
text = getTranscoder().decode(data);
} else if (url != null && !url.trim().isEmpty()) {
// Catch the last part of the URL if necessary
final Matcher m1 = urlPattern.matcher(url);
if (m1.find()) {
url = m1.group(1);
}
text = getTranscoder().decode(url);
}
return text;
}

private void redirectNow(HttpServletRequest request, HttpServletResponse response, String encoded)
throws IOException {
final String result = request.getContextPath() + "/uml/" + encoded;
response.sendRedirect(result);
}

private Transcoder getTranscoder() {
return TranscoderUtil.getDefaultTranscoder();
}
static private HttpURLConnection getConnection(URL url) throws IOException {
if (url.getProtocol().startsWith("https")) {
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setReadTimeout(10000); // 10 seconds
// printHttpsCert(con);
con.connect();
return con;
} else {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setReadTimeout(10000); // 10 seconds
con.connect();
return con;
}
}

static public InputStream getImage(URL url) throws IOException {
InputStream is = null;
HttpURLConnection con = getConnection(url);
is = con.getInputStream();
return is;
}



Expand Down

0 comments on commit 8569913

Please sign in to comment.