// Path: src/main/java/com/google/api/ads/dfp/appengine/oauth/AuthorizationCodeFlowFactory.java
// public class AuthorizationCodeFlowFactory {
// 
//   private final CredentialStore credentialStore;
//   private final HttpTransport httpTransport;
//   private final JsonFactory jsonFactory;
//   private final String clientId;
//   private final String clientSecret;
//   private final String scope;
// 
//   @Inject
//   public AuthorizationCodeFlowFactory(CredentialStore credentialStore,
//       JsonFactory jsonFactory,
//       HttpTransport httpTransport,
//       @Named("clientId") String clientId,
//       @Named("clientSecret") String clientSecret,
//       @Named("scope") String scope) {
//     this.credentialStore = credentialStore;
//     this.httpTransport = httpTransport;
//     this.jsonFactory = jsonFactory;
//     this.clientId = clientId;
//     this.clientSecret = clientSecret;
//     this.scope = scope;
//   }
// 
//   /**
//    * Returns a fetcher for an API object.
//    *
//    * @return a Google authorization code flow
//    */
//   public GoogleAuthorizationCodeFlow getInstance() {
//     Preconditions.checkNotNull(clientId);
//     Preconditions.checkNotNull(clientSecret);
//     return new GoogleAuthorizationCodeFlow.Builder(
//         httpTransport,
//         jsonFactory,
//         clientId,
//         clientSecret,
//         Lists.newArrayList(scope))
//         .setCredentialStore(credentialStore)
//         .setApprovalPrompt("force")
//         .setAccessType("offline")
//         .build();
//   }
// 
// }
// 
// Path: src/main/java/com/google/api/ads/dfp/appengine/oauth/CredentialException.java
// public class CredentialException extends Exception {
// 
//   /**
//    * Constructor.
//    *
//    * @param message the exception message
//    * @param exception the underlying exception
//    */
//   public CredentialException(String message, Throwable exception) {
//     super(message, exception);
//   }
// }
// 
// Path: src/main/java/com/google/api/ads/dfp/appengine/util/Networks.java
// public class Networks {
// 
//   private final Sessions sessions;
//   private final DfpServices dfpServices;
//   private final Predicate<Network> networkPredicate;
// 
//   /**
//    * Constructor.
//    *
//    * @param dfpServices provides DFP services
//    * @param sessions used to create a DFP session
//    * @param networkPredicate predicate used to filter networks
//    */
//   @Inject
//   public Networks(DfpServices dfpServices, Sessions sessions, Predicate<Network> networkPredicate) {
//     this.dfpServices = dfpServices;
//     this.sessions = sessions;
//     this.networkPredicate = networkPredicate;
//   }
// 
//   /**
//    * Gets a list of networks for the user, filtered according to the predicate.
//    *
//    * @param userId The app engine user ID
//    * @return a list of networks
//    * @throws ValidationException if the DFP session could not be created for the user
//    * @throws ApiException_Exception if API call fails
//    * @throws CredentialException if credential cannot be obtained
//    */
//   public List<Network> get(String userId) throws ValidationException, ApiException_Exception,
//       CredentialException {
//     DfpSession session = sessions.get(userId);
//     NetworkServiceInterface networkService =
//         dfpServices.get(session, NetworkServiceInterface.class);
//     return Lists.newArrayList(
//         Collections2.filter(networkService.getAllNetworks(), networkPredicate));
//   }
// 
//   /**
//    * Make a new test network.
//    *
//    * @param userId The app engine user ID
//    * @return the created network
//    * @throws ValidationException if the DFP session could not be created for the user
//    * @throws ApiException_Exception if API call fails
//    * @throws CredentialException if credential cannot be obtained
//    */
//   public Network make(String userId) throws ValidationException, ApiException_Exception,
//       CredentialException {
//     DfpSession session = sessions.get(userId);
//     NetworkServiceInterface networkService =
//         dfpServices.get(session, NetworkServiceInterface.class);
//     return networkService.makeTestNetwork();
//   }
// }


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.api.ads.common.lib.exception.ValidationException;
import com.google.api.ads.dfp.appengine.oauth.AuthorizationCodeFlowFactory;
import com.google.api.ads.dfp.appengine.oauth.CredentialException;
import com.google.api.ads.dfp.appengine.util.Networks;
import com.google.api.ads.dfp.jaxws.v201403.ApiException_Exception;
import com.google.api.ads.dfp.jaxws.v201403.Network;
import com.google.appengine.api.users.UserService;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
   */
  @Inject
  public CreateNetworkServlet(Networks networks, UserService userService,
      AuthorizationCodeFlowFactory authorizationCodeFlowFactory,
      @Named("redirectUrl") String redirectUrl) {
    super(authorizationCodeFlowFactory, redirectUrl);
    this.networks = networks;
    this.userService = userService;
  }

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    ServletContext servletContext = getServletContext();
    RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/newNetwork.jsp");
    requestDispatcher.forward(req, resp);
  }

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    ServletContext servletContext = getServletContext();
    RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/newNetwork.jsp");
    if (req.getParameter("createNetwork") != null) {
      try {
        Network newNetwork = networks.make(userService.getCurrentUser().getUserId());
        resp.sendRedirect("/index?newNetworkCode=" + newNetwork.getNetworkCode());
        return;
      } catch (ApiException_Exception e) {
        req.setAttribute("error", true);
// Path: src/main/java/com/google/api/ads/dfp/appengine/oauth/AuthorizationCodeFlowFactory.java
// public class AuthorizationCodeFlowFactory {
// 
//   private final CredentialStore credentialStore;
//   private final HttpTransport httpTransport;
//   private final JsonFactory jsonFactory;
//   private final String clientId;
//   private final String clientSecret;
//   private final String scope;
// 
//   @Inject
//   public AuthorizationCodeFlowFactory(CredentialStore credentialStore,
//       JsonFactory jsonFactory,
//       HttpTransport httpTransport,
//       @Named("clientId") String clientId,
//       @Named("clientSecret") String clientSecret,
//       @Named("scope") String scope) {
//     this.credentialStore = credentialStore;
//     this.httpTransport = httpTransport;
//     this.jsonFactory = jsonFactory;
//     this.clientId = clientId;
//     this.clientSecret = clientSecret;
//     this.scope = scope;
//   }
// 
//   /**
//    * Returns a fetcher for an API object.
//    *
//    * @return a Google authorization code flow
//    */
//   public GoogleAuthorizationCodeFlow getInstance() {
//     Preconditions.checkNotNull(clientId);
//     Preconditions.checkNotNull(clientSecret);
//     return new GoogleAuthorizationCodeFlow.Builder(
//         httpTransport,
//         jsonFactory,
//         clientId,
//         clientSecret,
//         Lists.newArrayList(scope))
//         .setCredentialStore(credentialStore)
//         .setApprovalPrompt("force")
//         .setAccessType("offline")
//         .build();
//   }
// 
// }
// 
// Path: src/main/java/com/google/api/ads/dfp/appengine/oauth/CredentialException.java
// public class CredentialException extends Exception {
// 
//   /**
//    * Constructor.
//    *
//    * @param message the exception message
//    * @param exception the underlying exception
//    */
//   public CredentialException(String message, Throwable exception) {
//     super(message, exception);
//   }
// }
// 
// Path: src/main/java/com/google/api/ads/dfp/appengine/util/Networks.java
// public class Networks {
// 
//   private final Sessions sessions;
//   private final DfpServices dfpServices;
//   private final Predicate<Network> networkPredicate;
// 
//   /**
//    * Constructor.
//    *
//    * @param dfpServices provides DFP services
//    * @param sessions used to create a DFP session
//    * @param networkPredicate predicate used to filter networks
//    */
//   @Inject
//   public Networks(DfpServices dfpServices, Sessions sessions, Predicate<Network> networkPredicate) {
//     this.dfpServices = dfpServices;
//     this.sessions = sessions;
//     this.networkPredicate = networkPredicate;
//   }
// 
//   /**
//    * Gets a list of networks for the user, filtered according to the predicate.
//    *
//    * @param userId The app engine user ID
//    * @return a list of networks
//    * @throws ValidationException if the DFP session could not be created for the user
//    * @throws ApiException_Exception if API call fails
//    * @throws CredentialException if credential cannot be obtained
//    */
//   public List<Network> get(String userId) throws ValidationException, ApiException_Exception,
//       CredentialException {
//     DfpSession session = sessions.get(userId);
//     NetworkServiceInterface networkService =
//         dfpServices.get(session, NetworkServiceInterface.class);
//     return Lists.newArrayList(
//         Collections2.filter(networkService.getAllNetworks(), networkPredicate));
//   }
// 
//   /**
//    * Make a new test network.
//    *
//    * @param userId The app engine user ID
//    * @return the created network
//    * @throws ValidationException if the DFP session could not be created for the user
//    * @throws ApiException_Exception if API call fails
//    * @throws CredentialException if credential cannot be obtained
//    */
//   public Network make(String userId) throws ValidationException, ApiException_Exception,
//       CredentialException {
//     DfpSession session = sessions.get(userId);
//     NetworkServiceInterface networkService =
//         dfpServices.get(session, NetworkServiceInterface.class);
//     return networkService.makeTestNetwork();
//   }
// }


// Path: src/main/java/com/google/api/ads/dfp/appengine/servlet/CreateNetworkServlet.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.api.ads.common.lib.exception.ValidationException;
import com.google.api.ads.dfp.appengine.oauth.AuthorizationCodeFlowFactory;
import com.google.api.ads.dfp.appengine.oauth.CredentialException;
import com.google.api.ads.dfp.appengine.util.Networks;
import com.google.api.ads.dfp.jaxws.v201403.ApiException_Exception;
import com.google.api.ads.dfp.jaxws.v201403.Network;
import com.google.appengine.api.users.UserService;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

   */
  @Inject
  public CreateNetworkServlet(Networks networks, UserService userService,
      AuthorizationCodeFlowFactory authorizationCodeFlowFactory,
      @Named("redirectUrl") String redirectUrl) {
    super(authorizationCodeFlowFactory, redirectUrl);
    this.networks = networks;
    this.userService = userService;
  }

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    ServletContext servletContext = getServletContext();
    RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/newNetwork.jsp");
    requestDispatcher.forward(req, resp);
  }

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    ServletContext servletContext = getServletContext();
    RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/newNetwork.jsp");
    if (req.getParameter("createNetwork") != null) {
      try {
        Network newNetwork = networks.make(userService.getCurrentUser().getUserId());
        resp.sendRedirect("/index?newNetworkCode=" + newNetwork.getNetworkCode());
        return;
      } catch (ApiException_Exception e) {
        req.setAttribute("error", true);
      } catch (ValidationException e) {