Package br.com.caelum.vraptor.restfulie.serialization

Source Code of br.com.caelum.vraptor.restfulie.serialization.LinkConverter

/***
* Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br
* All rights reserved.
*
* 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
*
* 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 br.com.caelum.vraptor.restfulie.serialization;

import br.com.caelum.vraptor.config.Configuration;
import br.com.caelum.vraptor.core.RequestInfo;
import br.com.caelum.vraptor.restfulie.Restfulie;
import br.com.caelum.vraptor.restfulie.hypermedia.HypermediaResource;
import br.com.caelum.vraptor.restfulie.relation.Relation;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

/**
* Reads all transitions from your resource object and converts them into link
* elements.<br>
* The converter passed in the constructor will be used to marshall the rest of
* the object.
*
* @author guilherme silveira
* @author lucas cavalcanti
*/
public class LinkConverter implements Converter {

  private final Restfulie restfulie;
  private final Converter base;
  private final Configuration config;

  public LinkConverter(Converter base, Restfulie restfulie, Configuration config) {
    this.base = base;
    this.restfulie = restfulie;
    this.config = config;
  }

  public void marshal(Object root, HierarchicalStreamWriter writer,
      MarshallingContext context) {
    base.marshal(root, writer, context);
    HypermediaResource resource = (HypermediaResource) root;
    try {
      for (Relation t : resource.getRelations(restfulie)) {
        writer.startNode("atom:link");
        writer.addAttribute("rel", t.getName());
        writer.addAttribute("href", config.getApplicationPath() + t.getUri());
        writer.addAttribute("xmlns:atom", "http://www.w3.org/2005/Atom");
        writer.endNode();
      }
    } finally {
      restfulie.clear();
    }
  }

  public Object unmarshal(HierarchicalStreamReader arg0,
      UnmarshallingContext arg1) {
    return base.unmarshal(arg0, arg1);
  }

  public boolean canConvert(Class type) {
    return HypermediaResource.class.isAssignableFrom(type)
        && base.canConvert(type);
  }

}
TOP

Related Classes of br.com.caelum.vraptor.restfulie.serialization.LinkConverter

TOP
Copyright © 2018 www.massapi.com. 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.