com.github.dreamhead.moco.handler.ContentHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moco-core Show documentation
Show all versions of moco-core Show documentation
Moco is an easy setup stub framework, mainly focusing on testing and integration.
package com.github.dreamhead.moco.handler;
import com.github.dreamhead.moco.HttpRequest;
import com.github.dreamhead.moco.MocoConfig;
import com.github.dreamhead.moco.Request;
import com.github.dreamhead.moco.ResponseHandler;
import com.github.dreamhead.moco.model.MessageContent;
import com.github.dreamhead.moco.resource.ContentResource;
import com.github.dreamhead.moco.resource.Resource;
import com.google.common.net.MediaType;
import static com.google.common.base.Optional.of;
public class ContentHandler extends AbstractContentResponseHandler {
private final ContentResource resource;
public ContentHandler(final ContentResource resource) {
this.resource = resource;
}
public ContentResource getResource() {
return resource;
}
@Override
protected MessageContent responseContent(final Request request) {
return this.resource.readFor(of(request));
}
@Override
protected MediaType getContentType(final HttpRequest request) {
return resource.getContentType(request);
}
@Override
public ResponseHandler apply(final MocoConfig config) {
ResponseHandler handler = super.apply(config);
if (handler != this) {
return handler;
}
Resource appliedResource = this.resource.apply(config);
if (appliedResource != this.resource) {
return new ContentHandler((ContentResource) appliedResource);
}
return this;
}
}