Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Dorađen rest server.

  • Loading branch information...
commit 2dbb8c36966eede39db03c1d0efbb88a650594b2 1 parent 31d2feb
@SandroGrzicic authored
Showing with 68 additions and 7 deletions.
  1. +68 −7 src/main/hr/fer/tel/ops/App.java
View
75 src/main/hr/fer/tel/ops/App.java
@@ -8,6 +8,11 @@
import spark.Request;
import spark.Response;
import spark.Route;
+import spark.Spark;
+import spark.route.SimpleRouteMatcher;
+
+import java.lang.reflect.Field;
+import java.util.List;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static spark.Spark.get;
@@ -19,10 +24,13 @@
private static final XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
+ private static String podržaneRute;
+
/** Pokreće web aplikaciju. */
public static void main(String[] args) {
final Server s = new Server();
+
// zahtjev za registracijom
get(new Route("/reg/:login/:pass") {
@Override
@@ -32,7 +40,7 @@ public Object handle(Request request, Response response) {
Either<String, String> odgovor = s.zahtjevZaRegistracijom(korisničkoIme, lozinka);
- return either2response(response, odgovor, SC_BAD_REQUEST);
+ return either2xml(response, odgovor, SC_BAD_REQUEST);
}
});
@@ -45,25 +53,78 @@ public Object handle(Request request, Response response) {
Either<String, String> odgovor = s.zahtjevZaUkidanjeRegistracije(korisničkoIme, lozinka);
- return either2response(response, odgovor, SC_BAD_REQUEST);
+ return either2xml(response, odgovor, SC_BAD_REQUEST);
+ }
+ });
+
+ // vraća broj registriranih korisnika.
+ get(new Route("/count") {
+ @Override
+ public Object handle(final Request request, final Response response) {
+ return string2xml(Integer.toString(s.getBrojKorisnika()));
+ }
+ });
+
+ podržaneRute = dohvatiRute();
+
+ // ispisuje sve podržane rute koristeći Reflection API.
+ get(new Route("/") {
+ @Override
+ public Object handle(final Request request, final Response response) {
+ return podržaneRute;
}
});
+ }
+
+ /** Vraća sve podržane rute koristeći Reflection. */
+ private static String dohvatiRute() {
+ final StringBuilder sb = new StringBuilder("REST metode: \n\n");
+ try {
+ final Field routeMatcher = Spark.class.getDeclaredField("routeMatcher");
+ routeMatcher.setAccessible(true);
+ final SimpleRouteMatcher simpleRouteMatcher = (SimpleRouteMatcher) routeMatcher.get(null);
+
+ final Field routesField = simpleRouteMatcher.getClass().getDeclaredField("routes");
+ routesField.setAccessible(true);
+
+ @SuppressWarnings("unchecked")
+ final List<?> routes = (List<?>) routesField.get(simpleRouteMatcher);
+ for (final Object route: routes) {
+ sb.append(route.toString().split(", ")[1]).append("\n");
+ }
+ return sb.toString();
+ } catch (Exception ignored) {
+ return ignored.getMessage();
+ }
+ }
+
+ /** Generira XML odgovor na temelju zadanog Stringa. */
+ private static String string2xml(final String string) {
+ final Document doc = new Document();
+ final Element root = new Element("ops");
+ doc.setRootElement(root);
+
+ final Element odgovorEl = new Element("odgovor");
+
+ odgovorEl.addContent(string);
+ root.addContent(odgovorEl);
+ return xmlOut.outputString(doc);
}
- /** Generira String odgovor na temelju zadanog Eithera. */
- private static String either2response(final Response httpResponse, final Either<String, String> odgovor, final int greškaKod) {
+ /** Generira XML odgovor na temelju zadanog Eithera. */
+ private static String either2xml(final Response httpResponse, final Either<String, String> either, final int greškaKod) {
final Document doc = new Document();
final Element root = new Element("ops");
doc.setRootElement(root);
- final Element odgovorEl = new Element(odgovor.getClass().getSimpleName().toLowerCase());
+ final Element odgovorEl = new Element(either.getClass().getSimpleName().toLowerCase());
- if (odgovor.isLeft()) {
+ if (either.isLeft()) {
httpResponse.status(greškaKod);
}
- odgovorEl.addContent(odgovor.toString());
+ odgovorEl.addContent(either.toString());
root.addContent(odgovorEl);
return xmlOut.outputString(doc);
}
Please sign in to comment.
Something went wrong with that request. Please try again.