forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestClientOptions.java
More file actions
120 lines (109 loc) · 3.68 KB
/
RequestClientOptions.java
File metadata and controls
120 lines (109 loc) · 3.68 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright 2011-2016 Amazon Technologies, Inc.
*
* 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://aws.amazon.com/apache2.0
*
* This file 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.amazonaws;
import java.io.InputStream;
import java.util.EnumMap;
import org.apache.http.annotation.NotThreadSafe;
/**
* Client request options such as client markers for individual
* {@link AmazonWebServiceRequest}s.
*/
@NotThreadSafe
public final class RequestClientOptions {
/**
* Used to enable mark-and-reset for
* non-mark-and-resettable non-file input stream for up to 128K memory
* buffering by default. Add 1 to get around an implementation quirk of
* BufferedInputStream.
*
* Retries after reading {@link #DEFAULT_STREAM_BUFFER_SIZE} bytes would
* fail to reset the underlying input stream as the mark position would
* have been invalidated.
*
*/
public static final int DEFAULT_STREAM_BUFFER_SIZE = (1 << 17)+1;
public static enum Marker {
/**
* Used to specify the http user_agent value.
* This marker is intended only for internal use by the AWS SDK.
*/
USER_AGENT,
;
}
private final EnumMap<Marker,String> markers = new EnumMap<Marker,String>(Marker.class);
/**
* Used for mark-and-reset purposes during retry.
*/
private int readLimit = DEFAULT_STREAM_BUFFER_SIZE;
/**
* Returns the value of the specified marker; or null if there is no such
* value.
*/
public String getClientMarker(Marker marker) {
return markers.get(marker);
}
/**
* Associates the given value with the given marker.
* Note the {@link Marker#USER_AGENT} is only intended for internal use
* by the AWS SDK.
*/
public void putClientMarker(Marker marker, String value) {
markers.put(marker, value);
}
/**
* Appends a user agent to the USER_AGENT client marker.
* This method is intended only for internal use by the AWS SDK.
*/
public void appendUserAgent(String userAgent) {
String marker = markers.get(Marker.USER_AGENT);
if (marker == null)
marker = "";
marker = createUserAgentMarkerString(marker, userAgent);
putClientMarker(Marker.USER_AGENT, marker);
}
/**
* Appends the given client marker string to the existing one and returns it.
*/
private String createUserAgentMarkerString(final String marker, String userAgent) {
return marker.contains(userAgent) ? marker : marker + " " + userAgent;
}
/**
* Returns the mark-and-reset read limit; defaults to
* {@value #DEFAULT_STREAM_BUFFER_SIZE}.
*
* @see InputStream#mark(int)
*/
public final int getReadLimit() {
return readLimit;
}
/**
* Sets the optional mark-and-reset read limit used for signing and retry
* purposes.
*
* @see InputStream#mark(int)
*/
public final void setReadLimit(int readLimit) {
this.readLimit = readLimit;
}
/**
* Copy the internal states of this <code>RequestClientOptions</code> to the
* target <code>RequestClientOptions</code>.
*/
void copyTo(RequestClientOptions target) {
target.setReadLimit(getReadLimit());
for (Marker marker: Marker.values())
target.putClientMarker(marker, getClientMarker(marker));
}
}