com.clouway.friendlyserve.RequiresParam Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fserve Show documentation
Show all versions of fserve Show documentation
Friendly Serving HTTP Library.
package com.clouway.friendlyserve;
import java.io.IOException;
import static com.google.common.base.Strings.isNullOrEmpty;
/**
* RequiresParam is a decorating class over {@link Fork} which checks whether the provided param is passed with the request.
*
*
* In cases when param is not passed, the route method is throwing {@link HttpException} with BAD_REQUEST value to indicate
* that some of the params is missing.
*
*
* @author Miroslav Genov ([email protected])
*/
public class RequiresParam implements Take {
private final String param;
private final Take origin;
public RequiresParam(String param, Take origin) {
this.param = param;
this.origin = origin;
}
@Override
public Response ack(Request request) throws IOException {
String paramValue = request.param(this.param);
if (isNullOrEmpty(paramValue)) {
return new RsBadRequest();
}
return origin.ack(request);
}
}