Skip to content

Commit

Permalink
Update web pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
jbernal committed Sep 27, 2018
1 parent 31258b1 commit 53fdb0a
Show file tree
Hide file tree
Showing 24 changed files with 214 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private void responseError(HttpResponse response, String msg) {
public void doGet(HttpRequest request, HttpResponse response) {
String result;
switch (request.getPath()) {
case "path1":
case "/path1":
// Injectar parámetros...
try {
int param2 = Integer.parseInt(request.getParams().get("param2"));
Expand All @@ -33,7 +33,7 @@ public void doGet(HttpRequest request, HttpResponse response) {
responseError(response, INTEGER_ERROR);
}
break;
case "path2":
case "/path2":
// Injectar parámetros...
result = resource2.method1();
response.setBody(result);
Expand All @@ -47,12 +47,12 @@ public void doGet(HttpRequest request, HttpResponse response) {
public void doPost(HttpRequest request, HttpResponse response) {
String result;
switch (request.getPath()) {
case "path1":
case "/path1":
// Injectar parámetros...
result = resource1.method2();
response.setBody(result);
break;
case "path1/sub":
case "/path1/sub":
// Injectar parámetros...
result = resource1.method3();
response.setBody(result);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/es/upm/miw/webpattern/http/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package es.upm.miw.webpattern.http;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Client {

private Server server;

public Client(Server server) {
this.server = server;
}

public HttpResponse submit(HttpRequest request) {
Logger logger = LogManager.getLogger(this.getClass());
logger.info(request.toString());
HttpResponse response = this.server.submit(request);
logger.info(response);
logger.info("---------------------------------------ooo----------------------------------------");
if (response.getStatus().isError()) {
throw new HttpException(response.getStatus(), response.getBody().toString());
}
return response;
}
}
26 changes: 0 additions & 26 deletions src/main/java/es/upm/miw/webpattern/http/HttpClientService.java

This file was deleted.

10 changes: 8 additions & 2 deletions src/main/java/es/upm/miw/webpattern/http/HttpException.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package es.upm.miw.webpattern.http;

public class HttpException extends RuntimeException {
private static final long serialVersionUID = 7408093310374440283L;
private static final long serialVersionUID = 7271728903247259709L;

public HttpException(String msg) {
private HttpStatus httpStatus;

public HttpException(HttpStatus httpStatus, String msg) {
super(msg);
this.httpStatus = httpStatus;
}

public HttpStatus getHttpStatus() {
return httpStatus;
}
}
102 changes: 89 additions & 13 deletions src/main/java/es/upm/miw/webpattern/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,33 @@ public class HttpRequest extends HttpBase {

private Map<String, String> queryParams;

public HttpRequest(String path, HttpMethod method) {
public HttpRequest(String path) {
this.path = path;
this.method = method;
this.method = HttpMethod.GET;
queryParams = new HashMap<>();
}

public HttpRequest() {
this("", HttpMethod.GET);
public static Builder builder(String path) {
return new Builder(path);
}

public String getPath(int order) {
return this.path.split("/")[order + 1];
}

public boolean isEqualsPath(String pathTemplate) {
String[] pathTemplateArray = pathTemplate.split("/");
String[] pathArray = this.path.split("/");
if (pathArray.length != pathTemplateArray.length) {
return false;
} else {
for (int i = 0; i < pathArray.length; i++) {
if (pathTemplateArray[i].indexOf('{') == -1 && !pathTemplateArray[i].equals(pathArray[i])) {
return false;
}
}
}
return true;
}

public String getPath() {
Expand All @@ -29,10 +48,6 @@ public void setPath(String path) {
this.path = path;
}

public String[] paths() {
return path.split("/");
}

public HttpMethod getMethod() {
return method;
}
Expand All @@ -53,11 +68,6 @@ public void clearQueryParams() {
queryParams.clear();
}

@Override
public String toString() {
return method.toString() + " /" + path + this.queryParams() + " " + super.toString();
}

private String queryParams() {
StringBuilder query = new StringBuilder();
String separator = "?";
Expand All @@ -71,4 +81,70 @@ private String queryParams() {
return query.toString();
}

@Override
public String toString() {
return this.method.toString() + ' ' + this.path + this.queryParams() + " " + super.toString();
}

public static class Builder {
private HttpRequest httpRequest;

private Builder(String path) {
this.httpRequest = new HttpRequest(path);
}

public Builder path(String path) {
this.httpRequest.path = httpRequest.getPath() + path;
return this;
}

public Builder expandPath(String value) {
httpRequest.path = httpRequest.path.substring(0, httpRequest.path.indexOf('{'))
+ value
+ httpRequest.path.substring(httpRequest.path.indexOf('}') + 1);
return this;
}

public Builder param(String key, String value) {
httpRequest.addQueryParam(key, value);
return this;
}

public Builder headerParam(String key, String value) {
httpRequest.addHeaderParam(key, value);
return this;
}

public Builder body(Object body) {
httpRequest.setBody(body);
return this;
}

public HttpRequest post() {
httpRequest.method = HttpMethod.POST;
return this.httpRequest;
}

public HttpRequest get() {
httpRequest.method = HttpMethod.GET;
return httpRequest;
}

public HttpRequest put() {
httpRequest.method = HttpMethod.PUT;
return httpRequest;
}

public HttpRequest patch() {
httpRequest.method = HttpMethod.PATCH;
return httpRequest;
}

public HttpRequest delete() {
httpRequest.method = HttpMethod.DELETE;
return httpRequest;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class AuthenticationFilter extends Filter {
public void doFilter(HttpRequest request, HttpResponse response, FilterChain filterChain) {
// PRE-PROCESS
LogManager.getLogger(this.getClass().getName()).info("Authenticating pre-process...");
if ("public".equals(request.paths()[0])) {
// si esta autorizado se continua con la cadena de filtros
filterChain.doFilter(request, response);
if ("public".equals(request.getPath(0))) {
filterChain.doFilter(request, response); // si esta autorizado se continua con la cadena de filtros
} else {
response.setStatus(HttpStatus.UNAUTHORIZED);
response.addHeaderParam("auth", "AuthenticationFilter");
response.setBody("Unsuccessful Authentication Filter");
// se aborta la cadena de filtros
}

// POST-PROCESS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class DebugFilter extends Filter {
public void doFilter(HttpRequest request, HttpResponse response, FilterChain filterChain) {
//PRE-PROCESS
LogManager.getLogger(this.getClass().getName()).info("Debuging pre-process...");

filterChain.doFilter(request, response);

//POST-PROCESS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,22 @@

public class FilterManager implements Server {

public void execute(HttpRequest request, HttpResponse response) {
// Se procesa el request y se establece el destino
FilterChain filterChain = new FilterChain(new Target());
@Override
public HttpResponse submit(HttpRequest request) {
HttpResponse response = new HttpResponse();

FilterChain filterChain = new FilterChain(new Target()); // Se procesa el request y se establece el destino

// Se procesa el request y se establece los filtros
// **/**
filterChain.addFilter(new AuthenticationFilter());
// **/public/**
if ("public".equals(request.paths()[0])) {
filterChain.addFilter(new AuthenticationFilter()); // **/** Siempre se aplica este filtro
if ("public".equals(request.getPath(0))) { // **/public/**
filterChain.addFilter(new TimeFilter());
}
// **/public/debug
if ("public/debug".equals(request.getPath())) {
if ("/public/debug".equals(request.getPath())) { // **/public/debug
filterChain.addFilter(new DebugFilter());
}

// Se pasa el control
filterChain.doFilter(request, response);
}
filterChain.doFilter(request, response); // Se pasa el control

@Override
public HttpResponse submit(HttpRequest request) {
HttpResponse response = new HttpResponse();
this.execute(request, response);
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public void doFilter(HttpRequest request, HttpResponse response, FilterChain fil
// PRE-PROCESS
long requestTime = new Date().getTime();
LogManager.getLogger(this.getClass().getName()).info("Time pre-process: " + new Date());

filterChain.doFilter(request, response);

// POST-PROCESS
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package es.upm.miw.pd.abstractfactory;

import org.junit.jupiter.api.Test;

import es.upm.miw.pd.abstractfactory.concrete1.Concrete1Factory;
import es.upm.miw.pd.abstractfactory.concrete2.Concrete2Factory;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -20,7 +19,7 @@ void testProductBOfConcrete1Factory() {
AbstractFactory.setAbstractFactory(new Concrete1Factory());
assertEquals("ProductB1", AbstractFactory.getAbstractFactory().getProductB().view());
}

@Test
void testProductAOfConcrete2Factory() {
AbstractFactory.setAbstractFactory(new Concrete2Factory());
Expand Down
16 changes: 7 additions & 9 deletions src/test/java/es/upm/miw/pd/builder/user/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

class UserTest {

@Test
void testUserIntStringStringStringIntIntStringListOfString(){
void testUserIntStringStringStringIntIntStringListOfString() {
List<String> tags = new ArrayList<>();
tags.add("Director");
tags.add("Socio");
Expand All @@ -29,13 +27,13 @@ void testUserIntStringStringStringIntIntStringListOfString(){
}

@Test
void testTagContainsFalseNullTags(){
void testTagContainsFalseNullTags() {
User user = new User(1, "Paco", null, null, -1, -1, null, null);
assertFalse(user.tagContains("Not"));
}

@Test
void testTagContainsFalse(){
void testTagContainsFalse() {
List<String> tags = new ArrayList<>();
tags.add("Director");
tags.add("Socio");
Expand All @@ -44,9 +42,9 @@ void testTagContainsFalse(){
user.setTags(tags);
assertFalse(user.tagContains("Not"));
}

@Test
void testTagContainsTrue(){
void testTagContainsTrue() {
List<String> tags = new ArrayList<>();
tags.add("Director");
tags.add("Socio");
Expand Down
Loading

0 comments on commit 53fdb0a

Please sign in to comment.