/gcm-server/src/main/java/com/google/android/apps/iosched/gcm/server/db/ApiKeyInitializer.java

https://gitlab.com/adam.lukaitis/iosched · Java · 94 lines · 55 code · 13 blank · 26 comment · 0 complexity · b196f5efe5599eee260081114ef38eb3 MD5 · raw file

  1. /*
  2. * Copyright 2014 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.android.apps.iosched.gcm.server.db;
  17. import java.util.logging.Logger;
  18. import javax.servlet.ServletContextEvent;
  19. import javax.servlet.ServletContextListener;
  20. import com.google.appengine.api.datastore.DatastoreServiceFactory;
  21. import com.google.appengine.api.datastore.Entity;
  22. import com.google.appengine.api.datastore.EntityNotFoundException;
  23. import com.google.appengine.api.datastore.Key;
  24. import com.google.appengine.api.datastore.KeyFactory;
  25. import com.google.appengine.api.datastore.DatastoreService;
  26. /**
  27. * Context initializer that loads the API key from a
  28. * {@value #PATH} file located in the classpath (typically under
  29. * {@code WEB-INF/classes}).
  30. */
  31. public class ApiKeyInitializer implements ServletContextListener {
  32. public static final String API_KEY = "<ENTER_YOUR_KEY>";
  33. public static final String ATTRIBUTE_ACCESS_KEY = "apiKey";
  34. private static final String ENTITY_KIND = "Settings";
  35. private static final String ENTITY_KEY = "MyKey";
  36. private static final String ACCESS_KEY_FIELD = "ApiKey";
  37. private final Logger mLogger = Logger.getLogger(getClass().getName());
  38. @Override
  39. public void contextInitialized(ServletContextEvent event) {
  40. DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  41. Key key = KeyFactory.createKey(ENTITY_KIND, ENTITY_KEY);
  42. Entity entity;
  43. try {
  44. entity = datastore.get(key);
  45. } catch(EntityNotFoundException e) {
  46. entity = new Entity(key);
  47. // NOTE: it's not possible to change entities in the local server, so
  48. // it will be necessary to hardcode the API key below if you are running
  49. // it locally.
  50. entity.setProperty(ACCESS_KEY_FIELD,
  51. API_KEY);
  52. datastore.put(entity);
  53. mLogger.severe("Created fake key. Please go to App Engine admin "
  54. + "console, change its value to your API Key (the entity "
  55. + "type is '" + ENTITY_KIND + "' and its field to be changed is '"
  56. + ACCESS_KEY_FIELD + "'), then restart the server!");
  57. }
  58. String accessKey = (String) entity.getProperty(ACCESS_KEY_FIELD);
  59. event.getServletContext().setAttribute(ATTRIBUTE_ACCESS_KEY, accessKey);
  60. }
  61. /**
  62. * Gets the access key.
  63. */
  64. protected String getKey() {
  65. com.google.appengine.api.datastore.DatastoreService datastore = DatastoreServiceFactory
  66. .getDatastoreService();
  67. Key key = KeyFactory.createKey(ENTITY_KIND, ENTITY_KEY);
  68. String apiKey = "";
  69. try {
  70. Entity entity = datastore.get(key);
  71. apiKey = (String) entity.getProperty(ACCESS_KEY_FIELD);
  72. } catch (EntityNotFoundException e) {
  73. mLogger.severe("Exception will retrieving the API Key"
  74. + e.toString());
  75. }
  76. return apiKey;
  77. }
  78. @Override
  79. public void contextDestroyed(ServletContextEvent event) {
  80. }
  81. }