1818
1919import com .google .api .server .spi .auth .EspAuthenticator ;
2020import com .google .api .server .spi .auth .common .User ;
21+ import com .google .api .server .spi .config .AnnotationBoolean ;
2122import 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 ;
2225import com .google .api .server .spi .config .ApiMethod ;
2326import 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 ;
2530
2631/** The Echo API which Endpoints will be exposing. */
32+ // [START echo_api_annotation]
2733@ Api (
2834 name = "echo" ,
2935 version = "v1" ,
3238 ownerDomain = "echo.example.com" ,
3339 ownerName = "echo.example.com" ,
3440 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]
3650 )
51+ // [END echo_api_annotation]
3752public class Echo {
3853 /**
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.
4056 *
4157 * Note that name is specified and will override the default name of "{class name}.{method
4258 * name}". For example, the default is "echo.echo".
4359 *
4460 * Note that httpMethod is not specified. This will default to a reasonable HTTP method
4561 * depending on the API method name. In this case, the HTTP method will default to POST.
4662 */
63+ // [START echo_method]
4764 @ 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+ }
49115 return message ;
50116 }
51117
@@ -59,19 +125,49 @@ public Message echo(Message message) {
59125 * Note that httpMethod is not required here. Without httpMethod, this will default to GET due
60126 * to the API method name. httpMethod is added here for example purposes.
61127 */
128+ // [START google_id_token_auth]
62129 @ ApiMethod (
63130 httpMethod = ApiMethod .HttpMethod .GET ,
64131 authenticators = {EspAuthenticator .class },
65132 audiences = {"YOUR_OAUTH_CLIENT_ID" },
66- authLevel = AuthLevel . REQUIRED
133+ clientIds = { "YOUR_OAUTH_CLIENT_ID" }
67134 )
68135 public Email getUserEmail (User user ) throws UnauthorizedException {
69136 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" );
71166 }
72167
73168 Email response = new Email ();
74169 response .setEmail (user .getEmail ());
75170 return response ;
76171 }
172+ // [END firebase_auth]
77173}
0 commit comments