forked from loafle/openapi-generator-original
committed by
wing328
parent
f00e6b349e
commit
bca35f6645
@@ -0,0 +1,27 @@
|
||||
package swagger;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import play.mvc.Action;
|
||||
import play.mvc.Http;
|
||||
import play.mvc.Result;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
public class ApiCall extends Action<SwaggerUtils.ApiAction> {
|
||||
|
||||
@Inject
|
||||
private ApiCall() {}
|
||||
|
||||
public CompletionStage<Result> call(Http.Context ctx) {
|
||||
try {
|
||||
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
|
||||
return delegate.call(ctx);
|
||||
} catch (Throwable t) {
|
||||
//TODO: log the error in your metric
|
||||
|
||||
//We rethrow this error so it will be caught in the ErrorHandler
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package swagger;
|
||||
|
||||
|
||||
import play.*;
|
||||
import play.api.OptionalSourceMapper;
|
||||
import play.api.UsefulException;
|
||||
import play.api.routing.Router;
|
||||
import play.http.DefaultHttpErrorHandler;
|
||||
import play.mvc.Http.*;
|
||||
import play.mvc.*;
|
||||
|
||||
import javax.inject.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import static play.mvc.Results.*;
|
||||
|
||||
@Singleton
|
||||
public class ErrorHandler extends DefaultHttpErrorHandler {
|
||||
|
||||
@Inject
|
||||
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
|
||||
super(configuration, environment, sourceMapper, routes);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
|
||||
return CompletableFuture.completedFuture(
|
||||
handleExceptions(exception)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
|
||||
return CompletableFuture.completedFuture(
|
||||
handleExceptions(exception)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void logServerError(RequestHeader request, UsefulException usefulException) {
|
||||
//Since the error is already handled, we don't want to print anything on the console
|
||||
//But if you want to have the error printed in the console, just delete this override
|
||||
}
|
||||
|
||||
private Result handleExceptions(Throwable t) {
|
||||
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
|
||||
return ok();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package swagger;
|
||||
|
||||
import play.mvc.With;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class SwaggerUtils {
|
||||
|
||||
@With(ApiCall.class)
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ApiAction {
|
||||
}
|
||||
|
||||
public static List<String> parametersToList(String collectionFormat, String[] values){
|
||||
List<String> params = new ArrayList<>();
|
||||
|
||||
if (values == null) {
|
||||
return params;
|
||||
}
|
||||
|
||||
if (values.length >= 1 && collectionFormat.equals("multi")) {
|
||||
params.addAll(Arrays.asList(values));
|
||||
} else {
|
||||
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
|
||||
|
||||
String delimiter = ",";
|
||||
|
||||
switch(collectionFormat) {
|
||||
case "csv": {
|
||||
delimiter = ",";
|
||||
break;
|
||||
}
|
||||
case "ssv": {
|
||||
delimiter = " ";
|
||||
break;
|
||||
}
|
||||
case "tsv": {
|
||||
delimiter = "\t";
|
||||
break;
|
||||
}
|
||||
case "pipes": {
|
||||
delimiter = "|";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
params = Arrays.asList(values[0].split(delimiter));
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
public static String parameterToString(Object param) {
|
||||
if (param == null) {
|
||||
return "";
|
||||
} else if (param instanceof Date) {
|
||||
return formatDatetime((Date) param);
|
||||
} else if (param instanceof Collection) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
for (Object o : (Collection)param) {
|
||||
if (b.length() > 0) {
|
||||
b.append(",");
|
||||
}
|
||||
b.append(String.valueOf(o));
|
||||
}
|
||||
|
||||
return b.toString();
|
||||
} else {
|
||||
return String.valueOf(param);
|
||||
}
|
||||
}
|
||||
|
||||
public static String formatDatetime(Date date) {
|
||||
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(date);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user