Ejemplo n.º 1
0
  /**
   * Can we handle a full stream.publish example?
   *
   * <p>See http://wiki.developers.facebook.com/index.php/Attachment_(Streams).
   */
  @Test
  public void streamPublish() throws JSONException {
    ActionLink category = new ActionLink();
    category.href = "http://bit.ly/KYbaN";
    category.text = "humor";

    Properties properties = new Properties();
    properties.category = category;
    properties.ratings = "5 stars";

    Medium medium = new Medium();
    medium.href = "http://bit.ly/187gO1";
    medium.src =
        "http://icanhascheezburger.files.wordpress.com/2009/03/funny-pictures-your-cat-is-bursting-with-joy1.jpg";
    medium.type = "image";

    List<Medium> media = new ArrayList<Medium>();
    media.add(medium);

    Attachment attachment = new Attachment();
    attachment.name = "i'm bursting with joy";
    attachment.href = "http://bit.ly/187gO1";
    attachment.caption = "{*actor*} rated the lolcat 5 stars";
    attachment.description = "a funny looking cat";
    attachment.properties = properties;
    attachment.media = media;

    String json = createJsonMapper().toJson(attachment);
    String expectedJson =
        "{\"description\":\"a funny looking cat\",\"name\":\"i'm bursting with joy\",\"caption\":\"{*actor*} rated the lolcat 5 stars\",\"properties\":{\"category\":{\"text\":\"humor\",\"href\":\"http://bit.ly/KYbaN\"},\"ratings\":\"5 stars\"},\"media\":[{\"src\":\"http://icanhascheezburger.files.wordpress.com/2009/03/funny-pictures-your-cat-is-bursting-with-joy1.jpg\",\"type\":\"image\",\"href\":\"http://bit.ly/187gO1\"}],\"href\":\"http://bit.ly/187gO1\"}";
    JSONAssert.assertEquals(expectedJson, json, JSONCompareMode.NON_EXTENSIBLE);
  }
  public List<Attachment> exportAttachments() {
    if (!this.running) return null;

    // get user configured column names and sql
    String colId = (String) this.properties.get(PROPKEY_ATTACHMENT_ID);
    String colThumb = (String) this.properties.get(PROPKEY_ATTACHMENT_THUMB);
    String colMessage = (String) this.properties.get(PROPKEY_ATTACHMENT_MESSAGE);
    String colName = (String) this.properties.get(PROPKEY_ATTACHMENT_NAME);
    String colHash = (String) this.properties.get(PROPKEY_ATTACHMENT_HASH);
    String table = (String) this.properties.get(PROPKEY_ATTACHMENT_TABLE);
    String rawSql = (String) this.properties.get(PROPKEY_ATTACHMENT_SQL);
    // update sql to reflect user configured column names
    String sql = rawSql.replaceAll("\\Q{" + PROPKEY_ATTACHMENT_ID + "}\\E", colId);
    sql = sql.replaceAll("\\Q{" + PROPKEY_ATTACHMENT_THUMB + "}\\E", colThumb);
    sql = sql.replaceAll("\\Q{" + PROPKEY_ATTACHMENT_MESSAGE + "}\\E", colMessage);
    sql = sql.replaceAll("\\Q{" + PROPKEY_ATTACHMENT_NAME + "}\\E", colName);
    sql = sql.replaceAll("\\Q{" + PROPKEY_ATTACHMENT_HASH + "}\\E", colHash);
    sql = sql.replaceAll("\\Q{" + PROPKEY_ATTACHMENT_TABLE + "}\\E", table);

    Vector<Attachment> attachments = new Vector<Attachment>();

    ResultSet data = null;
    try {
      data = sql(sql);
      while (data.next()) { // get each category
        Attachment attachment = new Attachment();
        attachment.id = data.getString(colId);
        attachment.thumb = data.getString(colThumb);
        attachment.message = data.getString(colMessage);
        attachment.name = encode(data.getBytes(colName));
        attachment.hash = data.getString(colHash);
        saveAttachmentWithId(attachment);
        attachments.add(attachment);
      }
    } catch (SQLException e) {
      log.error("Could not export Attachments with sql: " + sql);
      e.printStackTrace();
      return null;
    }
    return attachments;
  }
Ejemplo n.º 3
0
  private void addAttachments(Multipart multipart, List<Attachment> attachments)
      throws MessagingException {
    for (Attachment attachment : attachments) {
      BasicViewRenderer response = render(attachment.view());
      byte[] base64Encoded = Base64.encodeToByte(response.getOutputAsBytes());

      InternetHeaders headers = new InternetHeaders();
      headers.addHeader(Header.ContentType, response.getContentType());
      headers.addHeader(Header.ContentTransferEncoding, "base64");

      MimeBodyPart part = new MimeBodyPart(headers, base64Encoded);
      part.setFileName(attachment.name());
      part.setDisposition(attachment.disposition().value());

      if (attachment.isInline()) {
        part.setContentID(attachment.contentId());
      }

      multipart.addBodyPart(part);
    }
  }