1717
1818import com .fasterxml .jackson .databind .ObjectMapper ;
1919import com .google .api .client .util .Preconditions ;
20- import com .google .api .server .spi .Constant ;
2120import com .google .api .server .spi .ObjectMapperUtil ;
2221import com .google .api .server .spi .Strings ;
2322import com .google .api .server .spi .TypeLoader ;
3332import com .google .api .server .spi .config .model .Schema ;
3433import com .google .api .server .spi .config .model .Schema .Field ;
3534import com .google .api .server .spi .config .model .SchemaRepository ;
35+ import com .google .api .server .spi .config .model .AuthScopeRepository ;
3636import com .google .api .server .spi .config .model .StandardParameters ;
37+ import com .google .api .server .spi .config .scope .AuthScopeExpression ;
3738import com .google .api .server .spi .config .scope .AuthScopeExpressions ;
3839import com .google .api .services .discovery .model .DirectoryList ;
3940import com .google .api .services .discovery .model .DirectoryList .Items ;
4950import com .google .api .services .discovery .model .RestResource ;
5051import com .google .auto .value .AutoValue ;
5152import com .google .common .base .Function ;
52- import com .google .common .base .MoreObjects ;
5353import com .google .common .base .Splitter ;
5454import com .google .common .collect .ImmutableList ;
5555import com .google .common .collect .ImmutableListMultimap ;
6060import com .google .common .collect .Lists ;
6161import com .google .common .collect .Maps ;
6262import com .google .common .collect .Multimaps ;
63- import com .google .common .io .Resources ;
6463import com .google .common .reflect .TypeToken ;
65- import java .io .IOException ;
66- import java .io .InputStream ;
6764import java .net .MalformedURLException ;
6865import java .net .URL ;
6966import java .util .ArrayList ;
7067import java .util .Collection ;
7168import java .util .LinkedHashMap ;
7269import java .util .List ;
7370import java .util .Map ;
74- import java .util .Properties ;
75- import java .util .Set ;
71+ import java .util .Map .Entry ;
7672import java .util .TreeMap ;
77- import java .util .TreeSet ;
7873
7974/**
8075 * Generates discovery documents without contacting the discovery generator service.
@@ -92,20 +87,6 @@ public class DiscoveryGenerator {
9287 .setKind ("discovery#restDescription" )
9388 .setParameters (createStandardParameters ())
9489 .setProtocol ("rest" );
95- private static final Map <String , String > SCOPE_DESCRIPTIONS = loadScopeDescriptions ();
96-
97- private static Map <String , String > loadScopeDescriptions () {
98- try {
99- Properties properties = new Properties ();
100- URL resourceFile = Resources .getResource (DiscoveryGenerator .class , "scopeDescriptions.properties" );
101- InputStream inputStream = resourceFile .openStream ();
102- properties .load (inputStream );
103- inputStream .close ();
104- return Maps .fromProperties (properties );
105- } catch (IOException e ) {
106- throw new IllegalStateException ("Cannot load scope descriptions" , e );
107- }
108- }
10990
11091 private final TypeLoader typeLoader ;
11192
@@ -148,7 +129,7 @@ public Result writeDiscovery(
148129 }
149130
150131 private RestDescription writeApi (ApiKey apiKey , Iterable <ApiConfig > apiConfigs ,
151- DiscoveryContext context , SchemaRepository repo ) {
132+ DiscoveryContext context , SchemaRepository schemaRepo ) {
152133 // The first step is to scan all methods and try to extract a base path, aka a common prefix
153134 // for all methods. This prefix must end in a slash and can't contain any path parameters.
154135 String servicePath = computeApiServicePath (apiConfigs );
@@ -161,10 +142,8 @@ private RestDescription writeApi(ApiKey apiKey, Iterable<ApiConfig> apiConfigs,
161142 .setRootUrl (context .getApiRoot () + "/" )
162143 .setServicePath (servicePath )
163144 .setVersion (apiKey .getVersion ());
164- //stores scopes for all ApiConfigs and ApiMethodConfig, sorted alphabetically
165- Set <String > allScopes = new TreeSet <>();
166- //userinfo.email should always be requested, as it is required for authentication
167- allScopes .add (Constant .API_EMAIL_SCOPE );
145+
146+ final AuthScopeRepository scopeRepo = new AuthScopeRepository ();
168147
169148 for (ApiConfig config : apiConfigs ) {
170149 // API descriptions should be identical across all configs, but the last one will take
@@ -193,22 +172,21 @@ private RestDescription writeApi(ApiKey apiKey, Iterable<ApiConfig> apiConfigs,
193172 if (config .getCanonicalName () != null ) {
194173 doc .setCanonicalName (config .getCanonicalName ());
195174 }
196- allScopes . addAll ( AuthScopeExpressions . encode ( config .getScopeExpression () ));
175+ scopeRepo . add ( config .getScopeExpression ());
197176 for (ApiMethodConfig methodConfig : config .getApiClassConfig ().getMethods ().values ()) {
198177 if (!methodConfig .isIgnored ()) {
199- writeApiMethod (config , servicePath , doc , methodConfig , repo , allScopes );
178+ writeApiMethod (config , servicePath , doc , methodConfig , schemaRepo , scopeRepo );
200179 }
201180 }
202181 }
203182
204- LinkedHashMap <String , ScopesElement > scopeElements = new LinkedHashMap <>();
205- for (String scope : allScopes ) {
206- scopeElements .put (scope , new ScopesElement ().setDescription (
207- MoreObjects .firstNonNull (SCOPE_DESCRIPTIONS .get (scope ), scope )));
183+ Map <String , ScopesElement > scopeElements = new LinkedHashMap <>();
184+ for (Entry <String , String > entry : scopeRepo .getDescriptionsByScope ().entrySet ()) {
185+ scopeElements .put (entry .getKey (), new ScopesElement ().setDescription (entry .getValue ()));
208186 }
209187 doc .setAuth (new Auth ().setOauth2 (new Oauth2 ().setScopes (scopeElements )));
210188
211- List <Schema > schemas = repo .getAllSchemaForApi (apiKey );
189+ List <Schema > schemas = schemaRepo .getAllSchemaForApi (apiKey );
212190 if (!schemas .isEmpty ()) {
213191 Map <String , JsonSchema > docSchemas = Maps .newTreeMap ();
214192 for (Schema schema : schemas ) {
@@ -220,18 +198,18 @@ private RestDescription writeApi(ApiKey apiKey, Iterable<ApiConfig> apiConfigs,
220198 }
221199
222200 private void writeApiMethod (ApiConfig config , String servicePath , RestDescription doc ,
223- ApiMethodConfig methodConfig , SchemaRepository repo , Set < String > allScopes ) {
201+ ApiMethodConfig methodConfig , SchemaRepository schemaRepo , AuthScopeRepository scopeRepo ) {
224202 List <String > parts = DOT_SPLITTER .splitToList (methodConfig .getFullMethodName ());
225203 Map <String , RestMethod > methods = getMethodMapFromDoc (doc , parts );
226204 Map <String , JsonSchema > parameters = convertMethodParameters (methodConfig );
227- List < String > scopes = AuthScopeExpressions . encodeMutable ( methodConfig .getScopeExpression () );
205+ AuthScopeExpression scopeExpression = methodConfig .getScopeExpression ();
228206 RestMethod method = new RestMethod ()
229207 .setDescription (methodConfig .getDescription ())
230208 .setHttpMethod (methodConfig .getHttpMethod ())
231209 .setId (methodConfig .getFullMethodName ())
232210 .setPath (methodConfig .getCanonicalPath ().substring (servicePath .length ()))
233- .setScopes (scopes );
234- allScopes . addAll ( scopes );
211+ .setScopes (AuthScopeExpressions . encodeMutable ( scopeExpression ) );
212+ scopeRepo . add ( scopeExpression );
235213 List <String > parameterOrder = computeParameterOrder (methodConfig );
236214 if (!parameterOrder .isEmpty ()) {
237215 method .setParameterOrder (parameterOrder );
@@ -242,13 +220,13 @@ private void writeApiMethod(ApiConfig config, String servicePath, RestDescriptio
242220 ApiParameterConfig requestParamConfig = getAndCheckMethodRequestResource (methodConfig );
243221 if (requestParamConfig != null ) {
244222 TypeToken <?> requestType = requestParamConfig .getSchemaBaseType ();
245- Schema schema = repo .getOrAdd (requestType , config );
223+ Schema schema = schemaRepo .getOrAdd (requestType , config );
246224 method .setRequest (new Request ().set$ref (schema .name ()).setParameterName ("resource" ));
247225 }
248226 if (methodConfig .hasResourceInResponse ()) {
249227 TypeToken <?> returnType =
250228 ApiAnnotationIntrospector .getSchemaType (methodConfig .getReturnType (), config );
251- Schema schema = repo .getOrAdd (returnType , config );
229+ Schema schema = schemaRepo .getOrAdd (returnType , config );
252230 method .setResponse (new Response ().set$ref (schema .name ()));
253231 }
254232 methods .put (parts .get (parts .size () - 1 ), method );
0 commit comments