Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix a comment #810

Merged
merged 1 commit into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
/**
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine.analytics;

import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

import org.apache.http.client.utils.URIBuilder;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.client.utils.URIBuilder;

// [START example]
@SuppressWarnings("serial")
// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
@WebServlet(name = "analytics", description = "Analytics: Send Analytics Event to Google Analytics",
urlPatterns = "/analytics")
@WebServlet(
name = "analytics",
description = "Analytics: Send Analytics Event to Google Analytics",
urlPatterns = "/analytics"
)
public class AnalyticsServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
ServletException {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
String trackingId = System.getenv("GA_TRACKING_ID");
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("www.google-analytics.com").setPath("/collect")
builder
.setScheme("http")
.setHost("www.google-analytics.com")
.setPath("/collect")
.addParameter("v", "1") // API Version.
.addParameter("tid", trackingId) // Tracking ID / Property ID.
// Anonymous Client Identifier. Ideally, this should be a UUID that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,39 @@

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
@WebServlet(name = "UserAPI", description = "UserAPI: Login / Logout with UserService",
urlPatterns = "/userapi")
@WebServlet(
name = "UserAPI",
description = "UserAPI: Login / Logout with UserService",
urlPatterns = "/userapi"
)
public class UsersServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
UserService userService = UserServiceFactory.getUserService();

String thisUrl = req.getRequestURI();

resp.setContentType("text/html");
if (req.getUserPrincipal() != null) {
resp.getWriter().println("<p>Hello, "
+ req.getUserPrincipal().getName()
+ "! You can <a href=\""
+ userService.createLogoutURL(thisUrl)
+ "\">sign out</a>.</p>");
resp.getWriter()
.println(
"<p>Hello, "
+ req.getUserPrincipal().getName()
+ "! You can <a href=\""
+ userService.createLogoutURL(thisUrl)
+ "\">sign out</a>.</p>");
} else {
resp.getWriter().println("<p>Please <a href=\""
+ userService.createLoginURL(thisUrl)
+ "\">sign in</a>.</p>");
resp.getWriter()
.println(
"<p>Please <a href=\"" + userService.createLoginURL(thisUrl) + "\">sign in</a>.</p>");
}
}
}
// [END users_API_example]

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import static org.mockito.Mockito.when;

import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

import java.io.PrintWriter;
import java.io.StringWriter;
import javax.management.remote.JMXPrincipal;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -29,20 +33,11 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.management.remote.JMXPrincipal;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Unit tests for {@link UsersServlet}.
*/
/** Unit tests for {@link UsersServlet}. */
@RunWith(JUnit4.class)
public class UsersServletTest {
private static final String FAKE_URL = "fakey.fake.fak";
private static final String FAKE_NAME = "Fake";
private static final String FAKE_URL = "fakey.fake.fak";
private static final String FAKE_NAME = "Fake";
// Set up a helper so that the ApiProxy returns a valid environment for local testing.
private final LocalServiceTestHelper helper = new LocalServiceTestHelper();

Expand All @@ -65,7 +60,7 @@ public void setUp() throws Exception {
// If the user is logged in, use this request
when(mockRequestLoggedIn.getRequestURI()).thenReturn(FAKE_URL);
// Most of the classes that implement Principal have been
// deprecated. JMXPrincipal seems like a safe choice.
// deprecated. JMXPrincipal seems like a safe choice.
when(mockRequestLoggedIn.getUserPrincipal()).thenReturn(new JMXPrincipal(FAKE_NAME));

// Set up a fake HTTP response.
Expand All @@ -75,35 +70,34 @@ public void setUp() throws Exception {
servletUnderTest = new UsersServlet();
}

@After public void tearDown() {
@After
public void tearDown() {
helper.tearDown();
}

@Test
public void doGet_userNotLoggedIn_writesResponse() throws Exception {
servletUnderTest.doGet(mockRequestNotLoggedIn, mockResponse);

// If a user isn't logged in, we expect a prompt
// If a user isn't logged in, we expect a prompt
// to login to be returned.
assertThat(responseWriter.toString())
.named("UsersServlet response")
.contains("<p>Please <a href=");
assertThat(responseWriter.toString())
.named("UsersServlet response")
.contains("sign in</a>.</p>");
.contains("sign in</a>.</p>");
}

@Test
public void doGet_userLoggedIn_writesResponse() throws Exception {
servletUnderTest.doGet(mockRequestLoggedIn, mockResponse);

// If a user is logged in, we expect a prompt
// If a user is logged in, we expect a prompt
// to logout to be returned.
assertThat(responseWriter.toString())
.named("UsersServlet response")
.contains("<p>Hello, " + FAKE_NAME + "!");
assertThat(responseWriter.toString())
.named("UsersServlet response")
.contains("sign out");
assertThat(responseWriter.toString()).named("UsersServlet response").contains("sign out");
}
}
2 changes: 1 addition & 1 deletion flexible/postgres/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<user>root</user>
<password>myPassword</password>
<database>sqldemo</database>
<!-- [START_EXCLUDE] -->
<!-- [START_EXCLUDE silent] -->
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>

Expand Down