18
18
19
19
import com .google .api .server .spi .auth .EspAuthenticator ;
20
20
import com .google .api .server .spi .auth .common .User ;
21
+ import com .google .api .server .spi .config .AnnotationBoolean ;
21
22
import com .google .api .server .spi .config .Api ;
23
+ import com .google .api .server .spi .config .ApiIssuer ;
24
+ import com .google .api .server .spi .config .ApiIssuerAudience ;
22
25
import com .google .api .server .spi .config .ApiMethod ;
23
26
import com .google .api .server .spi .config .ApiNamespace ;
24
- import com .google .api .server .spi .config .AuthLevel ;
27
+ import com .google .api .server .spi .config .Named ;
28
+ import com .google .api .server .spi .config .Nullable ;
29
+ import com .google .api .server .spi .response .UnauthorizedException ;
25
30
26
31
/** The Echo API which Endpoints will be exposing. */
32
+ // [START echo_api_annotation]
27
33
@ Api (
28
34
name = "echo" ,
29
35
version = "v1" ,
32
38
ownerDomain = "echo.example.com" ,
33
39
ownerName = "echo.example.com" ,
34
40
packagePath = ""
35
- )
41
+ ),
42
+ // [START_EXCLUDE]
43
+ issuers = {
44
+ @ ApiIssuer (
45
+ name = "firebase" ,
46
+ issuer = "https://securetoken.google.com/YOUR-PROJECT-ID" ,
47
+ jwksUri = "https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com" )
48
+ }
49
+ // [END_EXCLUDE]
36
50
)
51
+ // [END echo_api_annotation]
37
52
public class Echo {
38
53
/**
39
- * Echoes the received message back.
54
+ * Echoes the received message back. If n is a non-negative integer, the message is copied that
55
+ * many times in the returned message.
40
56
*
41
57
* Note that name is specified and will override the default name of "{class name}.{method
42
58
* name}". For example, the default is "echo.echo".
43
59
*
44
60
* Note that httpMethod is not specified. This will default to a reasonable HTTP method
45
61
* depending on the API method name. In this case, the HTTP method will default to POST.
46
62
*/
63
+ // [START echo_method]
47
64
@ ApiMethod (name = "echo" )
48
- public Message echo (Message message ) {
65
+ public Message echo (Message message , @ Named ("n" ) @ Nullable Integer n ) {
66
+ return doEcho (message , n );
67
+ }
68
+ // [END echo_method]
69
+
70
+ /**
71
+ * Echoes the received message back. If n is a non-negative integer, the message is copied that
72
+ * many times in the returned message.
73
+ *
74
+ * Note that name is specified and will override the default name of "{class name}.{method
75
+ * name}". For example, the default is "echo.echo".
76
+ *
77
+ * Note that httpMethod is not specified. This will default to a reasonable HTTP method
78
+ * depending on the API method name. In this case, the HTTP method will default to POST.
79
+ */
80
+ // [START echo_path]
81
+ @ ApiMethod (name = "echo_path_parameter" , path = "echo/{n}" )
82
+ public Message echoPathParameter (Message message , @ Named ("n" ) int n ) {
83
+ return doEcho (message , n );
84
+ }
85
+ // [END echo_path]
86
+
87
+ /**
88
+ * Echoes the received message back. If n is a non-negative integer, the message is copied that
89
+ * many times in the returned message.
90
+ *
91
+ * Note that name is specified and will override the default name of "{class name}.{method
92
+ * name}". For example, the default is "echo.echo".
93
+ *
94
+ * Note that httpMethod is not specified. This will default to a reasonable HTTP method
95
+ * depending on the API method name. In this case, the HTTP method will default to POST.
96
+ */
97
+ // [START echo_api_key]
98
+ @ ApiMethod (name = "echo_api_key" , path = "echo_api_key" , apiKeyRequired = AnnotationBoolean .TRUE )
99
+ public Message echoApiKey (Message message , @ Named ("n" ) @ Nullable Integer n ) {
100
+ return doEcho (message , n );
101
+ }
102
+ // [END echo_api_key]
103
+
104
+ private Message doEcho (Message message , Integer n ) {
105
+ if (n != null && n >= 0 ) {
106
+ StringBuilder sb = new StringBuilder ();
107
+ for (int i = 0 ; i < n ; i ++) {
108
+ if (i > 0 ) {
109
+ sb .append (" " );
110
+ }
111
+ sb .append (message .getMessage ());
112
+ }
113
+ message .setMessage (sb .toString ());
114
+ }
49
115
return message ;
50
116
}
51
117
@@ -59,19 +125,49 @@ public Message echo(Message message) {
59
125
* Note that httpMethod is not required here. Without httpMethod, this will default to GET due
60
126
* to the API method name. httpMethod is added here for example purposes.
61
127
*/
128
+ // [START google_id_token_auth]
62
129
@ ApiMethod (
63
130
httpMethod = ApiMethod .HttpMethod .GET ,
64
131
authenticators = {EspAuthenticator .class },
65
132
audiences = {"YOUR_OAUTH_CLIENT_ID" },
66
- authLevel = AuthLevel . REQUIRED
133
+ clientIds = { "YOUR_OAUTH_CLIENT_ID" }
67
134
)
68
135
public Email getUserEmail (User user ) throws UnauthorizedException {
69
136
if (user == null ) {
70
- throw new UnauthorizedException ();
137
+ throw new UnauthorizedException ("Invalid credentials" );
138
+ }
139
+
140
+ Email response = new Email ();
141
+ response .setEmail (user .getEmail ());
142
+ return response ;
143
+ }
144
+ // [END google_id_token_auth]
145
+
146
+ /**
147
+ * Gets the authenticated user's email. If the user is not authenticated, this will return an HTTP
148
+ * 401.
149
+ *
150
+ * Note that name is not specified. This will default to "{class name}.{method name}". For
151
+ * example, the default is "echo.getUserEmail".
152
+ *
153
+ * Note that httpMethod is not required here. Without httpMethod, this will default to GET due
154
+ * to the API method name. httpMethod is added here for example purposes.
155
+ */
156
+ // [START firebase_auth]
157
+ @ ApiMethod (
158
+ path = "firebase_user" ,
159
+ httpMethod = ApiMethod .HttpMethod .GET ,
160
+ authenticators = {EspAuthenticator .class },
161
+ issuerAudiences = {@ ApiIssuerAudience (name = "firebase" , audiences = {"YOUR-PROJECT-ID" })}
162
+ )
163
+ public Email getUserEmailFirebase (User user ) throws UnauthorizedException {
164
+ if (user == null ) {
165
+ throw new UnauthorizedException ("Invalid credentials" );
71
166
}
72
167
73
168
Email response = new Email ();
74
169
response .setEmail (user .getEmail ());
75
170
return response ;
76
171
}
172
+ // [END firebase_auth]
77
173
}
0 commit comments