com.clouway.friendlyserve.RequiresHeader 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;
/**
* RequiresHeader is a decorator class over {@link Take} that checks whether the provided header is passed to the request
* or not. If it's not passed then bad request is returned as response otherwise request is passed to provided origin.
* This class is use-full as validation clause to verify that certain header is passed.
*
* @author Miroslav Genov ([email protected])
*/
public class RequiresHeader implements Take {
private final String headerName;
private final Take origin;
public RequiresHeader(String headerName, Take origin) {
this.headerName = headerName;
this.origin = origin;
}
@Override
public Response ack(Request request) throws IOException {
String headerValue = request.header(headerName);
if (isNullOrEmpty(headerValue)) {
return new RsBadRequest();
}
return origin.ack(request);
}
}