All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dreamhead.moco.handler.ContentHandler Maven / Gradle / Ivy

Go to download

Moco is an easy setup stub framework, mainly focusing on testing and integration.

There is a newer version: 1.5.0
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy