Java Code Examples for com.googlecode.objectify.ObjectifyService#ofy()

The following examples show how to use com.googlecode.objectify.ObjectifyService#ofy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeleteServlet.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
  String gameId = request.getParameter("gameKey");
  Objectify ofy = ObjectifyService.ofy();
  Game game = ofy.load().type(Game.class).id(gameId).safe();

  UserService userService = UserServiceFactory.getUserService();
  String currentUserId = userService.getCurrentUser().getUserId();

  // TODO(you): In practice, first validate that the user has permission to delete the Game
  game.deleteChannel(currentUserId);
}
 
Example 2
Source File: LiveRef.java    From gwt-jackson with Apache License 2.0 5 votes vote down vote up
/**
 * Get the current objectify instance associated with this ref
 */
private Objectify ofy() {
    // If we have an expired transaction context, we need a new context
    if ( ofy == null || (ofy.getTransaction() != null && !ofy.getTransaction().isActive()) ) {
        ofy = ObjectifyService.ofy();
    }

    return ofy;
}
 
Example 3
Source File: OpenedServletTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void doPost_open() throws Exception {
  // Insert a game
  Objectify ofy = ObjectifyService.ofy();
  Game game = new Game(USER_ID, "my-opponent", "         ", true);
  ofy.save().entity(game).now();
  String gameKey = game.getId();

  when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);

  // Mock out the firebase response. See
  // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
  MockHttpTransport mockHttpTransport =
      spy(
          new MockHttpTransport() {
            @Override
            public LowLevelHttpRequest buildRequest(String method, String url)
                throws IOException {
              return new MockLowLevelHttpRequest() {
                @Override
                public LowLevelHttpResponse execute() throws IOException {
                  MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                  response.setStatusCode(200);
                  return response;
                }
              };
            }
          });
  FirebaseChannel.getInstance().httpTransport = mockHttpTransport;

  servletUnderTest.doPost(mockRequest, mockResponse);

  verify(mockHttpTransport, times(2))
      .buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
 
Example 4
Source File: DeleteServletTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void doPost_deleteGame() throws Exception {
  // Insert a game
  Objectify ofy = ObjectifyService.ofy();
  Game game = new Game(USER_ID, "my-opponent", "         ", true);
  ofy.save().entity(game).now();
  String gameKey = game.getId();
  when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);

  // Mock out the firebase response. See
  // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
  MockHttpTransport mockHttpTransport =
      spy(
          new MockHttpTransport() {
            @Override
            public LowLevelHttpRequest buildRequest(String method, String url)
                throws IOException {
              return new MockLowLevelHttpRequest() {
                @Override
                public LowLevelHttpResponse execute() throws IOException {
                  MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                  response.setStatusCode(200);
                  return response;
                }
              };
            }
          });
  FirebaseChannel.getInstance().httpTransport = mockHttpTransport;

  servletUnderTest.doPost(mockRequest, mockResponse);

  verify(mockHttpTransport, times(1))
      .buildRequest(eq("DELETE"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
 
Example 5
Source File: MoveServletTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void doPost_myTurn_move() throws Exception {
  // Insert a game
  Objectify ofy = ObjectifyService.ofy();
  Game game = new Game(USER_ID, "my-opponent", "         ", true);
  ofy.save().entity(game).now();
  String gameKey = game.getId();

  when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
  when(mockRequest.getParameter("cell")).thenReturn("1");

  // Mock out the firebase response. See
  // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
  MockHttpTransport mockHttpTransport =
      spy(
          new MockHttpTransport() {
            @Override
            public LowLevelHttpRequest buildRequest(String method, String url)
                throws IOException {
              return new MockLowLevelHttpRequest() {
                @Override
                public LowLevelHttpResponse execute() throws IOException {
                  MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                  response.setStatusCode(200);
                  return response;
                }
              };
            }
          });
  FirebaseChannel.getInstance().httpTransport = mockHttpTransport;

  servletUnderTest.doPost(mockRequest, mockResponse);

  game = ofy.load().type(Game.class).id(gameKey).safe();
  assertThat(game.board).isEqualTo(" X       ");

  verify(mockHttpTransport, times(2))
      .buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
 
Example 6
Source File: MoveServletTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void doPost_notMyTurn_move() throws Exception {
  // Insert a game
  Objectify ofy = ObjectifyService.ofy();
  Game game = new Game(USER_ID, "my-opponent", "         ", false);
  ofy.save().entity(game).now();
  String gameKey = game.getId();

  when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
  when(mockRequest.getParameter("cell")).thenReturn("1");

  servletUnderTest.doPost(mockRequest, mockResponse);

  verify(mockResponse).sendError(401);
}
 
Example 7
Source File: OfyService.java    From tech-gallery with Apache License 2.0 4 votes vote down vote up
/**
 * Method that returns the objectify service reference.
 *
 * @return Objectify.
 */
public static Objectify ofy() {
  return ObjectifyService.ofy();
  // prior to v.4.0 use .begin() ,
  // since v.4.0 use ObjectifyService.ofy();
}
 
Example 8
Source File: OfyService.java    From watchpresenter with Apache License 2.0 4 votes vote down vote up
public static Objectify ofy() {
    return ObjectifyService.ofy();
}
 
Example 9
Source File: TicTacToeServlet.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  String gameKey = request.getParameter("gameKey");

  // 1. Create or fetch a Game object from the datastore
  Objectify ofy = ObjectifyService.ofy();
  Game game = null;
  String userId = UserServiceFactory.getUserService().getCurrentUser().getUserId();
  if (gameKey != null) {
    game = ofy.load().type(Game.class).id(gameKey).now();
    if (null == game) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }
    if (game.getUserO() == null && !userId.equals(game.getUserX())) {
      game.setUserO(userId);
    }
    ofy.save().entity(game).now();
  } else {
    // Initialize a new board. The board is represented as a String of 9 spaces, one for each
    // blank spot on the tic-tac-toe board.
    game = new Game(userId, null, "         ", true);
    ofy.save().entity(game).now();
    gameKey = game.getId();
  }

  // 2. Create this Game in the firebase db
  game.sendUpdateToClients();

  // 3. Inject a secure token into the client, so it can get game updates

  // [START pass_token]
  // The 'Game' object exposes a method which creates a unique string based on the game's key
  // and the user's id.
  String token = FirebaseChannel.getInstance().createFirebaseToken(game, userId);
  request.setAttribute("token", token);

  // 4. More general template values
  request.setAttribute("game_key", gameKey);
  request.setAttribute("me", userId);
  request.setAttribute("channel_id", game.getChannelKey(userId));
  request.setAttribute("initial_message", new Gson().toJson(game));
  request.setAttribute("game_link", getGameUriWithGameParam(request, gameKey));
  request.getRequestDispatcher("/WEB-INF/view/index.jsp").forward(request, response);
  // [END pass_token]
}
 
Example 10
Source File: RemoteApiClient.java    From teammates with GNU General Public License v2.0 4 votes vote down vote up
protected Objectify ofy() {
    return ObjectifyService.ofy();
}
 
Example 11
Source File: OfyService.java    From divide with Apache License 2.0 4 votes vote down vote up
protected static Objectify ofy() {
    return ObjectifyService.ofy();
}
 
Example 12
Source File: TicTacToeServletTest.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Test
public void doGet_existingGame() throws Exception {
  // Mock out the firebase response. See
  // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
  MockHttpTransport mockHttpTransport =
      spy(
          new MockHttpTransport() {
            @Override
            public LowLevelHttpRequest buildRequest(String method, String url)
                throws IOException {
              return new MockLowLevelHttpRequest() {
                @Override
                public LowLevelHttpResponse execute() throws IOException {
                  MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                  response.setStatusCode(200);
                  return response;
                }
              };
            }
          });
  FirebaseChannel.getInstance().httpTransport = mockHttpTransport;

  // Insert a game
  Objectify ofy = ObjectifyService.ofy();
  Game game = new Game("some-other-user-id", null, "         ", true);
  ofy.save().entity(game).now();
  String gameKey = game.getId();

  when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);

  servletUnderTest.doGet(mockRequest, mockResponse);

  // Make sure the game object was updated with the other player
  game = ofy.load().type(Game.class).first().safe();
  assertThat(game.userX).isEqualTo("some-other-user-id");
  assertThat(game.userO).isEqualTo(USER_ID);

  verify(mockHttpTransport, times(2))
      .buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
  verify(requestDispatcher).forward(mockRequest, mockResponse);
  verify(mockRequest).setAttribute(eq("token"), anyString());
  verify(mockRequest).setAttribute("game_key", game.id);
  verify(mockRequest).setAttribute("me", USER_ID);
  verify(mockRequest).setAttribute("channel_id", USER_ID + gameKey);
  verify(mockRequest).setAttribute(eq("initial_message"), anyString());
  verify(mockRequest).setAttribute(eq("game_link"), anyString());
}
 
Example 13
Source File: OfyService.java    From ud859 with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Use this static method for getting the Objectify service object in order to make sure the
 * above static block is executed before using Objectify.
 * @return Objectify service object.
 */
public static Objectify ofy() {
    return ObjectifyService.ofy();
}
 
Example 14
Source File: OfyService.java    From AdSearch_Endpoints with Apache License 2.0 2 votes vote down vote up
/**
 * Use this static method for getting the Objectify service object in order to make sure the
 * above static block is executed before using Objectify.
 * @return Objectify service object.
 */
public static Objectify ofy() {
    return ObjectifyService.ofy();
}
 
Example 15
Source File: OfyService.java    From ud859 with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Use this static method for getting the Objectify service object in order to make sure the
 * above static block is executed before using Objectify.
 * @return Objectify service object.
 */
public static Objectify ofy() {
    return ObjectifyService.ofy();
}
 
Example 16
Source File: OfyService.java    From ud859 with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Use this static method for getting the Objectify service object in order to make sure the
 * above static block is executed before using Objectify.
 * @return Objectify service object.
 */
public static Objectify ofy() {
    return ObjectifyService.ofy();
}
 
Example 17
Source File: OfyService.java    From ud859 with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Use this static method for getting the Objectify service object in order to make sure the
 * above static block is executed before using Objectify.
 * @return Objectify service object.
 */
public static Objectify ofy() {
    return ObjectifyService.ofy();
}
 
Example 18
Source File: OfyService.java    From ud859 with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Use this static method for getting the Objectify service object in order to make sure the
 * above static block is executed before using Objectify.
 * @return Objectify service object.
 */
public static Objectify ofy() {
    return ObjectifyService.ofy();
}
 
Example 19
Source File: OfyService.java    From ud859 with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Use this static method for getting the Objectify service object in order to make sure the
 * above static block is executed before using Objectify.
 * @return Objectify service object.
 */
public static Objectify ofy() {
    return ObjectifyService.ofy();
}
 
Example 20
Source File: OfyService.java    From MobileShoppingAssistant-sample with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the Objectify service wrapper.
 * @return The Objectify service wrapper.
 */
public static Objectify ofy() {
    return ObjectifyService.ofy();
}