aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/http/ContentRequest.java
blob: 4536bbad9f6b943156f87971188dd1948c82feb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http;

import com.yahoo.config.application.api.ApplicationFile;
import com.yahoo.container.jdisc.HttpRequest;
import ai.vespa.http.HttpURL.Path;

import java.io.InputStream;

import static com.yahoo.vespa.config.server.session.Session.Mode;

/**
 * Represents a {@link ContentRequest}, and contains common functionality for content requests for all content handlers.
 *
 * @author Ulf Lilleengen
 * @since 5.3
 */
public abstract class ContentRequest {
    private static final String RETURN_QUERY_PROPERTY = "return";

    enum ReturnType {CONTENT, STATUS}

    private final long sessionId;
    private final Path path;
    private final ApplicationFile file;
    private final HttpRequest request;

    protected ContentRequest(HttpRequest request, long sessionId, Path path, ApplicationFile applicationFile) {
        this.request = request;
        this.sessionId = sessionId;
        this.path = path;
        this.file = applicationFile;
    }

    public static Mode getApplicationFileMode(com.yahoo.jdisc.http.HttpRequest.Method method) {
        switch (method) {
            case GET:
            case OPTIONS:
                return Mode.READ;
            default:
                return Mode.WRITE;
        }
    }

    ReturnType getReturnType() {
        if (request.hasProperty(RETURN_QUERY_PROPERTY)) {
            String type = request.getProperty(RETURN_QUERY_PROPERTY);
            switch (type) {
                case "content":
                return ReturnType.CONTENT;
                case "status":
                return ReturnType.STATUS;
                default:
                throw new BadRequestException("return=" + type + " is an illegal argument. Only " +
                    ReturnType.CONTENT.name() + " and " + ReturnType.STATUS.name() + " are allowed");
            }
        } else {
            return ReturnType.CONTENT;
        }
    }

    protected abstract String getPathPrefix();

    String getUrlBase(String appendStr) {
        return Utils.getUrlBase(request, getPathPrefix() + appendStr);
    }

    boolean isRecursive() {
        return request.getBooleanProperty("recursive");
    }

    boolean hasRequestBody() {
        return request.getData() != null;
    }

    InputStream getData() {
        return request.getData();
    }


    Path getPath() {
        return path;
    }

    ApplicationFile getFile() {
        return file;
    }

    ApplicationFile getExistingFile() {
        if ( ! file.exists()) {
            throw new NotFoundException("Session " + sessionId + " does not contain a file at " + path);
        }
        return file;
    }
}