Salvatore La Manna
4 anni fa
9 ha cambiato i file con 298 aggiunte e 9 eliminazioni
@ -0,0 +1,165 @@
|
||||
package it.mwg.sicilia.sue.api.v1.command.impl; |
||||
|
||||
import it.mwg.sicilia.sue.api.v1.Parameters; |
||||
import it.mwg.sicilia.sue.api.v1.command.Command; |
||||
import it.mwg.sicilia.sue.api.v1.command.CommandList; |
||||
import it.mwg.sicilia.sue.api.v1.parameter.Parameter; |
||||
|
||||
import java.io.File; |
||||
import java.io.Serializable; |
||||
import java.util.ArrayList; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
import org.apache.commons.lang.StringUtils; |
||||
|
||||
import com.liferay.portal.kernel.json.JSONFactoryUtil; |
||||
import com.liferay.portal.kernel.json.JSONSerializer; |
||||
import com.liferay.portal.kernel.servlet.HttpMethods; |
||||
import com.liferay.portal.kernel.servlet.ServletResponseUtil; |
||||
import com.liferay.portal.kernel.util.Http; |
||||
import com.liferay.portal.kernel.util.StringPool; |
||||
|
||||
public class ManualHtml extends Command { |
||||
|
||||
@SuppressWarnings("serial") |
||||
private static final List<Parameter> OUTPUT_PARAMETERS = new ArrayList<Parameter>() { |
||||
{ |
||||
add(new Parameter(StringPool.BLANK, Parameter.TYPES.TEXT_HTML, "Il testo del manuale (questa pagina)")); |
||||
} |
||||
}; |
||||
|
||||
private static String baseUrl = null; |
||||
|
||||
private static final JSONSerializer serializer = JSONFactoryUtil.createJSONSerializer(); |
||||
|
||||
public ManualHtml(String verb, String description, String... methods) { |
||||
super(verb, description, methods); |
||||
} |
||||
|
||||
@Override |
||||
public void run(HttpServletRequest request, HttpServletResponse response) throws Exception { |
||||
|
||||
String cssFile = "/css/api/v1/api.css"; |
||||
StringBuilder result = new StringBuilder(); |
||||
result.append("<!doctype html>").append("<meta charset=\"utf-8\">") |
||||
.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">") |
||||
.append("<head><title>Manual</title>").append("<link rel=\"stylesheet\" href=\"") |
||||
.append(request.getContextPath()).append(cssFile).append("?v=") |
||||
.append(new File(request.getServletContext().getRealPath(cssFile)).lastModified()).append("\">") |
||||
.append("</head><body>"); |
||||
for (Command command : CommandList.getList()) { |
||||
result.append("<div class=\"command\">Comando: <b>/").append(command.getVerb()).append("</b>"); |
||||
if (command.isSecure()) { |
||||
result.append(" [T]"); |
||||
} |
||||
result.append("<div class=\"description\">").append(command.getDescription()).append("</div>"); |
||||
result.append("<div class=\"methods\">Metodi: ") |
||||
.append(StringUtils.join(command.getMethods(), StringPool.COMMA_AND_SPACE)).append("</div>"); |
||||
result.append("<div class=\"parameters\">Parametri:"); |
||||
List<Parameter> parameters = command.getInputParameters(); |
||||
if (parameters.isEmpty()) { |
||||
result.append(" Nessuno</div>"); |
||||
} else { |
||||
for (Parameter parameter : parameters) { |
||||
result.append("<div class=\"parameter\">").append(parameter.getName()); |
||||
if (parameter.isRequired()) { |
||||
result.append(StringPool.STAR); |
||||
} |
||||
result.append(" (").append(parameter.getType().toString()).append(") - ") |
||||
.append(parameter.getDescription()).append("</div>"); |
||||
} |
||||
result.append("</div>"); |
||||
} |
||||
parameters = command.getOutputParameters(); |
||||
if (!parameters.isEmpty()) { |
||||
result.append("<div class=\"returns\">Restituisce:"); |
||||
for (Parameter parameter : parameters) { |
||||
result.append("<div class=\"parameter\">").append(parameter.getName()).append(" (") |
||||
.append(parameter.getType().toString()).append(") - ").append(parameter.getDescription()); |
||||
if (parameter.getType().equals(Parameter.TYPES.ARRAY)) { |
||||
for (Parameter subParameter : parameter.getSubParameters()) { |
||||
result.append("<div class=\"parameter\">").append(subParameter.getName()).append(" (") |
||||
.append(subParameter.getType().toString()).append(") - ") |
||||
.append(subParameter.getDescription()).append("</div>"); |
||||
} |
||||
} |
||||
result.append("</div>"); |
||||
} |
||||
} |
||||
result.append("</div>"); |
||||
result.append("<div class=\"example\">Esempio:<pre>").append(getCurlExample(command, getBaseUrl(request))) |
||||
.append("</pre></div>"); |
||||
result.append("</div>"); // class="command"
|
||||
} |
||||
result.append("<div class=\"notes\">Note:<br>[T] = Richiede token di autenticazione: header di richiesta \"") |
||||
.append(Parameters.X_AUTH_TOKEN).append("\" oppure parametro \"").append(Parameters.TOKEN) |
||||
.append("\"<br>* = Parametro obbligatorio</div>"); |
||||
result.append("</body></html>"); |
||||
setTextHtml(response); |
||||
ServletResponseUtil.write(response, result.toString()); |
||||
} |
||||
|
||||
private String getCurlExample(Command command, String baseUrl) { |
||||
|
||||
StringBuilder sb = new StringBuilder(); |
||||
sb.append("curl "); |
||||
if (command.isSecure()) { |
||||
sb.append("--header \"").append(Parameters.X_AUTH_TOKEN).append(": $TOKEN\"").append(" \\\n\t"); |
||||
} |
||||
if (command.getMethods().contains(HttpMethods.GET)) { |
||||
} else { |
||||
sb.append("--header \"Content-Type: application/json; charset=UTF-8\" \\\n\t"); |
||||
sb.append("--request POST \\\n\t"); |
||||
Map<String, Serializable> data = new LinkedHashMap<>(); |
||||
for (Parameter parameter : command.getInputParameters()) { |
||||
if (parameter.isRequired()) { |
||||
if (parameter.getType().equals(Parameter.TYPES.STRING)) { |
||||
data.put(parameter.getName(), parameter.getName()); |
||||
} |
||||
} |
||||
} |
||||
if (data.size() > 0) { |
||||
sb.append("--data '").append(serializer.serialize(data)).append("' \\\n\t"); |
||||
} |
||||
} |
||||
sb.append(StringPool.APOSTROPHE).append(baseUrl).append(command.getVerb()).append(StringPool.APOSTROPHE) |
||||
.append(StringPool.NEW_LINE); |
||||
return sb.toString(); |
||||
} |
||||
|
||||
private String getBaseUrl(HttpServletRequest request) { |
||||
|
||||
if (null == baseUrl) { |
||||
StringBuilder sb = new StringBuilder(); |
||||
sb.append(request.isSecure() ? Http.HTTPS_WITH_SLASH : Http.HTTP_WITH_SLASH); |
||||
sb.append(request.getServerName()); |
||||
if ((request.isSecure() && (request.getServerPort() != Http.HTTPS_PORT)) |
||||
|| (!request.isSecure() && (request.getServerPort() != Http.HTTP_PORT))) { |
||||
sb.append(StringPool.COLON).append(request.getServerPort()); |
||||
} |
||||
sb.append(request.getContextPath()).append(request.getServletPath()).append(StringPool.FORWARD_SLASH); |
||||
baseUrl = sb.toString(); |
||||
} |
||||
return baseUrl; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isSecure() { |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public List<Parameter> getInputParameters() { |
||||
return EMPTY_PARAMETERS; |
||||
} |
||||
|
||||
@Override |
||||
public List<Parameter> getOutputParameters() { |
||||
return OUTPUT_PARAMETERS; |
||||
} |
||||
} |
@ -0,0 +1,70 @@
|
||||
/* http://meyerweb.com/eric/tools/css/reset/ |
||||
v2.0 | 20110126 |
||||
License: none (public domain) */ |
||||
html, body, div, span, applet, object, iframe, |
||||
h1, h2, h3, h4, h5, h6, p, blockquote, pre, |
||||
a, abbr, acronym, address, big, cite, code, |
||||
del, dfn, em, img, ins, kbd, q, s, samp, |
||||
small, strike, strong, sub, sup, tt, var, |
||||
b, u, i, center, |
||||
dl, dt, dd, ol, ul, li, |
||||
fieldset, form, label, legend, |
||||
table, caption, tbody, tfoot, thead, tr, th, td, |
||||
article, aside, canvas, details, embed, |
||||
figure, figcaption, footer, header, hgroup, |
||||
menu, nav, output, ruby, section, summary, |
||||
time, mark, audio, video { |
||||
margin: 0; |
||||
padding: 0; |
||||
border: 0; |
||||
font-size: 100%; |
||||
font: inherit; |
||||
vertical-align: baseline; |
||||
} |
||||
/* HTML5 display-role reset for older browsers */ |
||||
article, aside, details, figcaption, figure, |
||||
footer, header, hgroup, menu, nav, section { |
||||
display: block; |
||||
} |
||||
body { |
||||
line-height: 1; |
||||
} |
||||
ol, ul { |
||||
list-style: none; |
||||
} |
||||
blockquote, q { |
||||
quotes: none; |
||||
} |
||||
blockquote:before, blockquote:after, |
||||
q:before, q:after { |
||||
content: ''; |
||||
content: none; |
||||
} |
||||
table { |
||||
border-collapse: collapse; |
||||
border-spacing: 0; |
||||
} |
||||
/* End reset.css */ |
||||
body { |
||||
margin: 10px; |
||||
font-family: sans-serif; |
||||
} |
||||
div { |
||||
margin-bottom: 5px; |
||||
} |
||||
b { |
||||
font-weight: bold; |
||||
} |
||||
pre { |
||||
background-color: #ccc; |
||||
width: fit-content; |
||||
padding: 10px; |
||||
border: 1px solid #999; |
||||
} |
||||
.command { |
||||
border-bottom: 1px solid #999; |
||||
padding: 10px 0; |
||||
} |
||||
.parameter { |
||||
margin-left: 40px; |
||||
} |
Caricamento…
Reference in new issue