|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package com.google.api.gax.httpjson; |
| 31 | + |
| 32 | +import com.google.api.core.BetaApi; |
| 33 | +import com.google.api.pathtemplate.PathTemplate; |
| 34 | +import com.google.protobuf.Message; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +/** Creates parts of a HTTP request from a protobuf message. */ |
| 39 | +@BetaApi |
| 40 | +public class ProtoMessageRequestFormatter<RequestT extends Message> |
| 41 | + implements HttpRequestFormatter<RequestT> { |
| 42 | + |
| 43 | + private final FieldsExtractor<RequestT, String> requestBodyExtractor; |
| 44 | + // Using of triple nested generics (which is not pretty) is predetermined by the |
| 45 | + // Map<String, List<String>> returned value type of the getQueryParamNames interface method |
| 46 | + // implemented by this class. |
| 47 | + private final FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor; |
| 48 | + private final PathTemplate pathTemplate; |
| 49 | + private final FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor; |
| 50 | + |
| 51 | + private ProtoMessageRequestFormatter( |
| 52 | + FieldsExtractor<RequestT, String> requestBodyExtractor, |
| 53 | + FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor, |
| 54 | + PathTemplate pathTemplate, |
| 55 | + FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor) { |
| 56 | + this.requestBodyExtractor = requestBodyExtractor; |
| 57 | + this.queryParamsExtractor = queryParamsExtractor; |
| 58 | + this.pathTemplate = pathTemplate; |
| 59 | + this.pathVarsExtractor = pathVarsExtractor; |
| 60 | + } |
| 61 | + |
| 62 | + public static <RequestT extends Message> |
| 63 | + ProtoMessageRequestFormatter.Builder<RequestT> newBuilder() { |
| 64 | + return new Builder<>(); |
| 65 | + } |
| 66 | + |
| 67 | + /* {@inheritDoc} */ |
| 68 | + @Override |
| 69 | + public Map<String, List<String>> getQueryParamNames(RequestT apiMessage) { |
| 70 | + return queryParamsExtractor.extract(apiMessage); |
| 71 | + } |
| 72 | + |
| 73 | + /* {@inheritDoc} */ |
| 74 | + @Override |
| 75 | + public String getRequestBody(RequestT apiMessage) { |
| 76 | + return requestBodyExtractor.extract(apiMessage); |
| 77 | + } |
| 78 | + |
| 79 | + /* {@inheritDoc} */ |
| 80 | + @Override |
| 81 | + public String getPath(RequestT apiMessage) { |
| 82 | + return pathTemplate.instantiate(pathVarsExtractor.extract(apiMessage)); |
| 83 | + } |
| 84 | + |
| 85 | + /* {@inheritDoc} */ |
| 86 | + @Override |
| 87 | + public PathTemplate getPathTemplate() { |
| 88 | + return pathTemplate; |
| 89 | + } |
| 90 | + |
| 91 | + // This has class has compound setter methods (multiple arguments in setters), that is why not |
| 92 | + // using @AutoValue. |
| 93 | + public static class Builder<RequestT extends Message> { |
| 94 | + private FieldsExtractor<RequestT, String> requestBodyExtractor; |
| 95 | + private FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor; |
| 96 | + private String path; |
| 97 | + private FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor; |
| 98 | + |
| 99 | + public Builder<RequestT> setRequestBodyExtractor( |
| 100 | + FieldsExtractor<RequestT, String> requestBodyExtractor) { |
| 101 | + this.requestBodyExtractor = requestBodyExtractor; |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + public Builder<RequestT> setQueryParamsExtractor( |
| 106 | + FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor) { |
| 107 | + this.queryParamsExtractor = queryParamsExtractor; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public Builder<RequestT> setPath( |
| 112 | + String path, FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor) { |
| 113 | + this.path = path; |
| 114 | + this.pathVarsExtractor = pathVarsExtractor; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + public ProtoMessageRequestFormatter<RequestT> build() { |
| 119 | + return new ProtoMessageRequestFormatter<>( |
| 120 | + requestBodyExtractor, queryParamsExtractor, PathTemplate.create(path), pathVarsExtractor); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments