예제 #1
0
  /**
   * Bootstrap a new user. Do all of the typical actions for a new user:
   *
   * <ul>
   *   <li>Creating a timeline subscription
   *   <li>Inserting a contact
   *   <li>Sending the user a welcome message
   * </ul>
   */
  public static void bootstrapNewUser(HttpServletRequest req, String userId) throws IOException {
    Credential credential = AuthUtil.newAuthorizationCodeFlow().loadCredential(userId);

    // Create contact
    Contact starterProjectContact = new Contact();
    starterProjectContact.setId(MainServlet.CONTACT_ID);
    starterProjectContact.setDisplayName(MainServlet.CONTACT_NAME);
    starterProjectContact.setImageUrls(
        Lists.newArrayList(WebUtil.buildUrl(req, "/static/images/chipotle-tube-640x360.jpg")));
    starterProjectContact.setAcceptCommands(
        Lists.newArrayList(new Command().setType("TAKE_A_NOTE")));
    Contact insertedContact = MirrorClient.insertContact(credential, starterProjectContact);
    LOG.info("Bootstrapper inserted contact " + insertedContact.getId() + " for user " + userId);

    try {
      // Subscribe to timeline updates
      Subscription subscription =
          MirrorClient.insertSubscription(
              credential, WebUtil.buildUrl(req, "/notify"), userId, "timeline");
      LOG.info(
          "Bootstrapper inserted subscription " + subscription.getId() + " for user " + userId);
    } catch (GoogleJsonResponseException e) {
      LOG.warning(
          "Failed to create timeline subscription. Might be running on "
              + "localhost. Details:"
              + e.getDetails().toPrettyString());
    }

    // Send welcome timeline item
    TimelineItem timelineItem = FeedItem.createSimpleTextTimeLineItem("Welcome to Glass Feed!");
    TimelineItem insertedItem = MirrorClient.insertTimelineItem(credential, timelineItem);
    LOG.info(
        "Bootstrapper inserted welcome message " + insertedItem.getId() + " for user " + userId);
  }
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String attachmentId = req.getParameter("attachment");
    String timelineItemId = req.getParameter("timelineItem");
    if (attachmentId == null || timelineItemId == null) {
      LOG.warning("attempted to load image attachment with missing IDs");
      resp.sendError(400);
    }
    // identify the viewing user
    Credential credential = AuthUtil.getCredential(req);

    // Get the content type
    String contentType =
        MirrorClient.getAttachmentContentType(credential, timelineItemId, attachmentId);

    // Get the attachment bytes
    InputStream attachmentInputStream =
        MirrorClient.getAttachmentInputStream(credential, timelineItemId, attachmentId);

    // Write it out
    resp.setContentType(contentType);
    ByteStreams.copy(attachmentInputStream, resp.getOutputStream());
  }