Package org.apache.abdera.writer

Examples of org.apache.abdera.writer.StreamWriter


*/
public class EntityProviderExample {

    public static void main(String... args) throws Exception {
        Abdera abdera = Abdera.getInstance();
        StreamWriter sw = abdera.newStreamWriter().setOutputStream(System.out).setAutoIndent(true);
        Foo foo = new Foo();
        foo.writeTo(sw);
        sw.close();
    }
View Full Code Here


        return (T)getAbdera().newStreamWriter();
    }

    public <T extends StreamWriter> T newStreamWriter(String name) {
        Class<? extends StreamWriter> _class = getStreamWriters().get(name);
        StreamWriter sw = null;
        if (_class != null) {
            try {
                sw = _class.newInstance();
            } catch (Exception e) {
                throw new RuntimeException(Localizer.sprintf("IMPLEMENTATION.NOT.AVAILABLE", "StreamWriter"), e);
View Full Code Here

        if (response.isCommitted()) {
            log.error("Could not write an error message as the headers & HTTP status were already committed!");
        } else {
            response.setStatus(500);
            StreamWriter sw = getAbdera().newStreamWriter().setOutputStream(response.getOutputStream(), "UTF-8");
            Error.create(sw, 500, message, t);
            sw.close();
        }
    }
View Full Code Here

    return (T)getAbdera().newStreamWriter();
  }

  public <T extends StreamWriter> T newStreamWriter(String name) {
    Class<? extends StreamWriter> _class = getStreamWriters().get(name);
    StreamWriter sw = null;
    if (_class != null) {
      try {
        sw = _class.newInstance();
      } catch (Exception e) {
        throw new RuntimeException(
View Full Code Here

            } else {
                // private post
                entry.setRights(Common.RIGHTS_RESERVED);
                try {
                    StringWriter stringWriter = new StringWriter();
                    StreamWriter writer = Abdera.getInstance()
                            .getWriterFactory().newStreamWriter();
                    writer.setWriter(stringWriter);
                    writer.startEntry();
                    writer.writeId(entry.getId());
                    writer.writeUpdated(entry.getUpdated());
                    writer.writePublished(entry.getPublished());
                    if (predecessor != null) {
                        writer.startElement(Common.PREDECESSOR, Common.NS_URI);
                        writer.writeElementText(predecessor);
                        writer.endElement();
                    }
                    if (options.publicOptions != null) {
                        // these are options that will be publicly visible
                        if (options.publicOptions.status != null) {
                            writer.writeTitle(options.publicOptions.status);
                        } else {
                            writer.writeTitle(""); // empty title
                        }
                        if (options.publicOptions.body != null) {
                            writer.writeSummary(options.publicOptions.body);
                        }
                        if (options.publicOptions.verb != null) {
                            writer.startElement("verb",
                                    "http://activitystrea.ms/spec/1.0/");
                            writer.writeElementText(options.publicOptions.verb);
                            writer.endElement();
                        }
                        if (options.publicOptions.tags != null) {
                            for (String s : options.publicOptions.tags) {
                                writer.writeCategory(s);
                            }
                        }
                        if (options.publicOptions.mentions != null) {
                            for (String s : options.publicOptions.mentions) {
                                writer.startElement("mention", Common.NS_URI,
                                        "trsst");
                                writer.writeElementText(s);
                                writer.endElement();
                            }
                        }
                    } else {
                        writer.writeTitle(""); // empty title
                    }

                    writer.startContent("application/xenc+xml");

                    List<PublicKey> keys = new LinkedList<PublicKey>();
                    for (String id : options.recipientIds) {
                        // for each recipient
                        Feed recipientFeed = pull(id);
                        if (recipientFeed != null) {
                            // fetch encryption key
                            Element e = recipientFeed.getExtension(new QName(
                                    Common.NS_URI, Common.ENCRYPT));
                            if (e == null) {
                                // fall back to signing key
                                e = recipientFeed.getExtension(new QName(
                                        Common.NS_URI, Common.SIGN));
                            }
                            keys.add(Common.toPublicKeyFromX509(e.getText()));
                        }
                    }

                    // enforce the convention:
                    keys.remove(encryptionKeys.getPublic());
                    // move to end if exists;
                    // last encrypted key is for ourself
                    keys.add(encryptionKeys.getPublic());

                    // encrypt content key separately for each recipient
                    for (PublicKey recipient : keys) {
                        byte[] bytes = Crypto.encryptKeyWithIES(contentKey,
                                feed.getUpdated().getTime(), recipient,
                                encryptionKeys.getPrivate());
                        String encoded = new Base64(0, null, true)
                                .encodeToString(bytes);
                        writer.startElement("EncryptedData",
                                "http://www.w3.org/2001/04/xmlenc#");
                        writer.startElement("CipherData",
                                "http://www.w3.org/2001/04/xmlenc#");
                        writer.startElement("CipherValue",
                                "http://www.w3.org/2001/04/xmlenc#");
                        writer.writeElementText(encoded);
                        writer.endElement();
                        writer.endElement();
                        writer.endElement();
                    }

                    // now: encrypt the payload with content key
                    byte[] bytes = encryptElementAES(entry, contentKey);
                    String encoded = new Base64(0, null, true)
                            .encodeToString(bytes);
                    writer.startElement("EncryptedData",
                            "http://www.w3.org/2001/04/xmlenc#");
                    writer.startElement("CipherData",
                            "http://www.w3.org/2001/04/xmlenc#");
                    writer.startElement("CipherValue",
                            "http://www.w3.org/2001/04/xmlenc#");
                    writer.writeElementText(encoded);
                    writer.endElement();
                    writer.endElement();
                    writer.endElement();

                    // done with encrypted elements
                    writer.endContent();
                    writer.endEntry();
                    writer.flush();
                    // this constructed entry now replaces the encrypted
                    // entry
                    entry = (Entry) Abdera.getInstance().getParserFactory()
                            .getParser()
                            .parse(new StringReader(stringWriter.toString()))
View Full Code Here

  protected void process(
    Object source,
    ObjectContext objectContext,
    SerializationContext context,
    Conventions conventions) {
      StreamWriter sw = context.getStreamWriter();
      if (!(source instanceof Element)) return;
      Element element = (Element) source;
      sw.startElement(element.getQName());
      for (QName attr : element.getAttributes())
        sw.writeAttribute(
          attr,
          element.getAttributeValue(attr));
      XPath xpath = context.getAbdera().getXPath();
      List<?> children = xpath.selectNodes("node()", element);
      for (Object child : children) {
        if (child instanceof Element) {
          process(child, new ObjectContext(child), context, conventions);
        } else if (child instanceof Comment) {
          Comment comment = (Comment) child;
          sw.writeComment(comment.getText());
        } else if (child instanceof ProcessingInstruction) {
          ProcessingInstruction pi = (ProcessingInstruction) child;
          sw.writePI(pi.getText(), pi.getTarget());
        } else if (child instanceof TextValue) {
          TextValue tv = (TextValue) child;
          sw.writeElementText(tv.getText());
        }
      }     
      sw.endElement();
  }
View Full Code Here

      type = _text != null ? _text.type() : type;
      contentValue = source;
      valueContext = objectContext;
    }
    QName qname = this.qname != null ? this.qname : getQName(objectContext.getAccessor());
    StreamWriter sw = context.getStreamWriter();
    sw.startText(qname,type);
    writeAttributes(
        source,
        objectContext,
        context,
        conventions);

    switch(type) {
      case TEXT:
      case HTML:
        sw.writeElementText(toString(contentValue));
        break;
      case XHTML:
        Div div = null;
        if (contentValue instanceof Div)
          div = (Div) contentValue;
View Full Code Here

    Object source,
    ObjectContext objectContext,
    SerializationContext context,
    Conventions conventions) {
   
    StreamWriter sw = context.getStreamWriter();
    Category _category = objectContext.getAnnotation(Category.class);
   
    String scheme = null;
    AccessibleObject accessor = objectContext.getAccessor(Scheme.class, conventions);
    if (accessor != null) {
      Object value = eval(accessor, source);
      if (value != null)
        scheme = toString(value);
    }
    if (scheme == null) {
      Scheme _scheme = objectContext.getAnnotation(Scheme.class);
      if (_scheme != null && !_scheme.value().equals(DEFAULT)) {
        scheme = _scheme.value();
      }
    }
    if (scheme == null && _category != null && !_category.scheme().equals(DEFAULT)) {
      scheme = _category.scheme();
    }
    if (scheme != null)
      sw.writeAttribute("scheme", scheme);

    String label = null;
    accessor = objectContext.getAccessor(Label.class, conventions);
    if (accessor != null) {
      Object value = eval(accessor, source);
      if (value != null)
        label = toString(value);
    }
    if (label == null) {
      Label _label = objectContext.getAnnotation(Label.class);
      if (_label != null && !_label.value().equals(DEFAULT)) {
        label = _label.value();
      }
    }
    if (label == null && _category != null && !_category.label().equals(DEFAULT)) {
      label = _category.label();
    }
    if (label != null)
      sw.writeAttribute("label", label);
   
    String term = null;
    accessor = objectContext.getAccessor(Value.class, conventions);
    if (accessor != null) {
      Object value = eval(accessor, source);
      if (value != null)
        term = toString(value);
    }
    if (term == null) term = toString(source);
    if (term != null)
      sw.writeAttribute("term", term);
   
    writeAttributes(source, objectContext, context, conventions);
    writeExtensions(source, objectContext, context, conventions);
  }
View Full Code Here

    Object source,
    ObjectContext objectContext,
    SerializationContext context,
    Conventions conventions) {
      QName qname = this.qname != null ? this.qname : getQName(objectContext.getAccessor());
      StreamWriter sw = context.getStreamWriter();
      sw.startElement(qname);
  }
View Full Code Here

        type = _content != null ? _content.type() : type;
        contentValue = source;
        valueContext = objectContext;
      }
     
      StreamWriter sw = context.getStreamWriter();
      sw.startContent(type);
      writeAttributes(
          source,
          objectContext,
          context,
          conventions);

      if (type == Type.MEDIA || type == Type.XML) {
      String mediatype = null;
      AccessibleObject mtaccessor = valueContext.getAccessor(MediaType.class, conventions);
      if (mtaccessor != null) {
        Object mtvalue = eval(mtaccessor,contentValue);
        mediatype = mtvalue != null ? toString(mtvalue) : null;
      }
      if (mediatype == null) {
        MediaType mt = valueContext.getAnnotation(MediaType.class);
        mediatype = mt != null && !mt.value().equals(DEFAULT) ? mt.value() : null;
      }
      if (mediatype != null)
        sw.writeAttribute("type", mediatype);
      }
     
      switch(type) {
        case TEXT:
        case HTML:
          sw.writeElementText(toString(contentValue));
          break;
        case XHTML:
          Div div = null;
          if (contentValue instanceof Div)
            div = (Div) contentValue;
          else {
            div = context.getAbdera().getFactory().newDiv();
            div.setValue(toString(contentValue));
          }
          context.serialize(div, new ObjectContext(div));
          break;
        case XML:
          Element el = null;
          if (contentValue instanceof Element)
            el = (Element) contentValue;
          else {
            StringReader sr = new StringReader(toString(contentValue));
            Document<Element> doc = context.getAbdera().getParser().parse(sr);
            el = doc.getRoot();
          }
          context.serialize(el, new ObjectContext(el));
          break;
        case MEDIA:
          try {
            if (contentValue instanceof DataHandler)
              sw.writeElementText((DataHandler)contentValue);
            else if (contentValue instanceof InputStream)
              sw.writeElementText((InputStream)contentValue);
            else
              sw.writeElementText(toString(contentValue));
          } catch (IOException e) {
            throw new SerializationException(e);
          }
      }     
  }
View Full Code Here

TOP

Related Classes of org.apache.abdera.writer.StreamWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.