Package com.couchace.core.spi.http

Source Code of com.couchace.core.spi.http.HttpRequestFactory

/*
* Copyright 2012 Harlan Noonkester
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.couchace.core.spi.http;

import com.couchace.core.api.CouchException;
import com.couchace.core.api.http.CouchMediaType;
import com.couchace.core.api.query.CouchPageQuery;
import com.couchace.core.api.query.CouchViewQuery;
import com.couchace.core.api.request.*;
import com.couchace.core.internal.util.UriUtil;
import com.couchace.core.spi.json.CouchJsonStrategy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* User: harlan
* Date: 2/8/14
* Time: 12:27 PM
*/
public class HttpRequestFactory {

    private final String databaseName;
    private final CouchJsonStrategy jsonStrategy;

    public HttpRequestFactory(String databaseName, CouchJsonStrategy jsonStrategy) {
        this.databaseName = databaseName;
        this.jsonStrategy = jsonStrategy;
    }

    public <T extends HttpReadRequest> T newHttpReadRequest(Class<T> httpReadRequestClass, ReadRequest request) {

        List<HttpQueryParam> queryParameters = new ArrayList<>();
        String path;
        CouchMediaType acceptType = CouchMediaType.APPLICATION_JSON;
        if (request instanceof GetAttachmentRequest) {
            // Attachment
            GetAttachmentRequest getAttachmentRequest = (GetAttachmentRequest) request;
            path = UriUtil.buildPath(databaseName, request.getDocumentId(), getAttachmentRequest.getAttachmentName());
            acceptType = null;
            if (request.getDocumentRevision() != null) {
                HttpQueryParam queryParam = new HttpQueryParam("rev", request.getDocumentRevision());
                queryParameters = Collections.singletonList(queryParam);
            }
        } else if (request.getDocumentId() != null) {
            // Document id
            path = UriUtil.buildPath(databaseName, request.getDocumentId());
            if (request.getDocumentRevision() != null) {
                HttpQueryParam queryParam = new HttpQueryParam("rev", request.getDocumentRevision());
                queryParameters = Collections.singletonList(queryParam);
            }
        } else if (request.getViewQuery() != null) {
            // View query
            CouchViewQuery viewQuery = request.getViewQuery();
            path = UriUtil.buildPath(
                databaseName,
                "_design",
                viewQuery.getDesignName(),
                "_view",
                viewQuery.getViewName());

            queryParameters.add(new HttpQueryParam("include_docs", String.valueOf(viewQuery.isIncludeDocs())));
            if (viewQuery.hasKey()) {
                queryParameters.add(new HttpQueryParam("key", viewQuery.getKeyJson()));
            }
            if (viewQuery.hasStartKey()) {
                queryParameters.add(new HttpQueryParam("startkey", viewQuery.getStartKeyJson()));
            }
            if (viewQuery.hasEndKey()) {
                queryParameters.add(new HttpQueryParam("endkey", viewQuery.getEndKeyJson()));
            }
            if (viewQuery.getLimit() > 0) {
                queryParameters.add(new HttpQueryParam("limit", String.valueOf(viewQuery.getLimit() + 1)));
            }
            if (viewQuery.isDescending()) {
                queryParameters.add(new HttpQueryParam("descending", String.valueOf(viewQuery.isDescending())));
            }

        } else if (request.getPageQuery() != null) {
            // Page query
            CouchPageQuery pageQuery = request.getPageQuery();
            path = UriUtil.buildPath(databaseName, pageQuery.getRequestedPage());
            queryParameters.add(new HttpQueryParam("include_docs", String.valueOf(pageQuery.isIncludeDocs())));
            queryParameters.add(new HttpQueryParam("limit", String.valueOf(pageQuery.getPageSize() + 1)));

        } else {
            // Unsupported
            throw CouchException.badRequest("ReadRequest did not supply a document id, view query or page query");
        }

        if (httpReadRequestClass.isAssignableFrom(HttpGetRequest.class)) {
            return httpReadRequestClass.cast(new HttpGetRequest(path, queryParameters, request.getDocumentId(), acceptType));

        } else if (httpReadRequestClass.isAssignableFrom(HttpHeadRequest.class)) {
            return httpReadRequestClass.cast(new HttpHeadRequest(path, queryParameters, request.getDocumentId()));

        } else {
            throw CouchException.badRequest("Unsupported HttpReadRequest type " + httpReadRequestClass);
        }

    }

    public HttpPostRequest newHttpPostRequest(PostRequest request) {

        String path = databaseName;
        if (request instanceof PostEntityRequest) {
            // Create json document.
            String json = jsonStrategy.createJsonForPost((PostEntityRequest) request);
            return new HttpPostRequest(path, json);

        } else if (request instanceof PostDocumentRequest) {
            PostDocumentRequest postDocumentRequest = (PostDocumentRequest) request;
            String json = postDocumentRequest.getDocument();
            return new HttpPostRequest(path, json);

        } else {
            throw CouchException.badRequest("Unsupported PostRequest type " + request.getClass());
        }

    }

    public HttpPutRequest newHttpPutRequest(PutRequest request) {
        if (request instanceof PutEntityRequest) {
            return putEntity((PutEntityRequest) request);

        } else if (request instanceof PutAttachmentRequest) {
            return putAttachment((PutAttachmentRequest) request);

        } else if (request instanceof PutDocumentRequest) {
            return putDocument((PutDocumentRequest) request);

        } else if (request instanceof PutDesignRequest) {
            return putDesign((PutDesignRequest) request);

        } else {
            throw CouchException.badRequest("Unsupported PutRequest type " + request.getClass());
        }
    }

    public HttpDeleteRequest newHttpDeleteRequest(DeleteRequest request) {

        List<HttpQueryParam> queryParameters = null;
        String path;
        if (request.getType() == DeleteRequest.Type.DATABASE) {
            path = UriUtil.buildPath(databaseName);

        } else {
            path = UriUtil.buildPath(databaseName, request.getDocumentId());
            if (request.getDocumentRevision() != null) {
                HttpQueryParam queryParam = new HttpQueryParam("rev", request.getDocumentRevision());
                queryParameters = Collections.singletonList(queryParam);
            }
        }

        return new HttpDeleteRequest(path, request.getDocumentId(), queryParameters);
    }

    protected HttpPutRequest putEntity(PutEntityRequest request) {

        // Create json document.
        String json = jsonStrategy.createJsonForPut(request);

        List<HttpQueryParam> queryParameters = null;
        if (request.getDocumentRevision() != null) {
            HttpQueryParam queryParam = new HttpQueryParam("rev", request.getDocumentRevision());
            queryParameters = Collections.singletonList(queryParam);
        }

        String path = UriUtil.buildPath(databaseName, request.getDocumentId());
        return new HttpPutRequest(
            path,
            queryParameters,
            request.getDocumentId(),
            CouchMediaType.APPLICATION_JSON,
            json);
    }

    protected HttpPutRequest putDesign(PutDesignRequest request) {
        String designContent = request.getDesignContent();
        String designName = request.getDesignName();
        List<HttpQueryParam> queryParameters = null;
        if (request.getDocumentRevision() != null) {
            HttpQueryParam queryParam = new HttpQueryParam("rev", request.getDocumentRevision());
            queryParameters = Collections.singletonList(queryParam);
        }
        String path = UriUtil.buildPath(databaseName, "_design", designName);
        return new HttpPutRequest(
            path,
            queryParameters,
            designName,
            CouchMediaType.APPLICATION_JSON,
            designContent);
    }

    protected HttpPutRequest putDocument(PutDocumentRequest request) {
        String documentId = request.getDocumentId();
        List<HttpQueryParam> queryParameters = null;
        if (request.getDocumentRevision() != null) {
            HttpQueryParam queryParam = new HttpQueryParam("rev", request.getDocumentRevision());
            queryParameters = Collections.singletonList(queryParam);
        }
        String path = UriUtil.buildPath(databaseName, documentId);
        return new HttpPutRequest(
            path,
            queryParameters,
            documentId,
            CouchMediaType.APPLICATION_JSON,
            request.getDocument());
    }

    protected HttpPutRequest putAttachment(PutAttachmentRequest request) {
        String path = UriUtil.buildPath(databaseName, request.getDocumentId(), request.getAttachmentName());
        String documentId = request.getDocumentId();
        List<HttpQueryParam> queryParameters = null;
        if (request.getDocumentRevision() != null) {
            HttpQueryParam queryParam = new HttpQueryParam("rev", request.getDocumentRevision());
            queryParameters = Collections.singletonList(queryParam);
        }

        Object content = (request.getContent() != null) ? request.getContent() : null;

        return new HttpPutRequest(
            path,
            queryParameters,
            documentId,
            request.getContentType(),
            content);
    }

}
TOP

Related Classes of com.couchace.core.spi.http.HttpRequestFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.