com.github.dreamhead.moco.rest.RestHandler 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.rest;
import com.github.dreamhead.moco.HttpRequest;
import com.github.dreamhead.moco.MocoConfig;
import com.github.dreamhead.moco.MutableHttpResponse;
import com.github.dreamhead.moco.ResponseHandler;
import com.github.dreamhead.moco.RestSetting;
import com.github.dreamhead.moco.handler.AbstractHttpResponseHandler;
import com.github.dreamhead.moco.internal.SessionContext;
import com.google.common.base.Optional;
public class RestHandler extends AbstractHttpResponseHandler {
private final RestRequestDispatcher dispatcher;
private final String name;
private final Iterable settings;
public RestHandler(final String name, final Iterable settings) {
this.name = name;
this.dispatcher = new RestRequestDispatcher(name, settings);
this.settings = settings;
}
@Override
protected void doWriteToResponse(final HttpRequest httpRequest, final MutableHttpResponse httpResponse) {
Optional responseHandler = dispatcher.getResponseHandler(httpRequest);
if (responseHandler.isPresent()) {
responseHandler.get().writeToResponse(new SessionContext(httpRequest, httpResponse));
return;
}
throw new UnsupportedOperationException("Unsupported REST request");
}
@Override
@SuppressWarnings("unchecked")
public ResponseHandler apply(final MocoConfig config) {
if (config.isFor(MocoConfig.URI_ID)) {
return new RestHandler((String) config.apply(name), settings);
}
return super.apply(config);
}
}