wiki:RuConnecting

Version 3 (modified by heri, 14 years ago) (diff)

--

Connecting a Resource User

This topic will describe a short client for the Rest Interfaces available in Sensei. All Sensei components have TestPut/Get files that you can use as examples. We will focus on a small Java client.
The Restlet client library we use is that from Noelios Technologies and it's version 2. (http://www.restlet.org/about/).

Small Example Java client

/**
 * <p>Resource Directory Helper class.</p>
 * 
 * @author Heri
 *
 */
public class RDClient extends Client {

    /**
     * <p>Logging component</p>
     */
    private static final transient Logger logger =
            Logger.getLogger("ro.upb.sensei.gui.clients.rd.RDClient");
    /**
     * <p>RD location</p>
     */
    private String url;

    /**
     * <p>Default constructor</p>
     * 
     * @param protocol  Protocol to use
     */
    public RDClient(Protocol protocol, String url) {
        super(protocol);
        this.url = url;
    }

    /**
     * <p>Add a resource description</p>
     * 
     * @param e Resource Description to add
     * @return  If the operation failed or not
     * @throws DAOException
     */
    public String addResourceDescription(
            ResourceDescription e) throws DAOException {
        if (e == null) {
            throw new DAOException("I will not add an empty resource description.");
        }

        StringWriter buf = new StringWriter();
        JAXB.marshal(e, buf);
        String xmlRepresentation = buf.toString();

        return addResourceDescription(xmlRepresentation);
    }

    /**
     * <p>Add a resource description</p>
     * 
     * @param xmlRepresentation Resource Description to add in String format
     * @throws DAOException
     */
    public String addResourceDescription(String xmlRepresentation)
            throws DAOException {
        if (xmlRepresentation == null) {
            throw new DAOException("I will not add an empty resource description.");
        }

        String retVal = null;

        Reference itemsUri = new Reference(url + "/rpi");
        StringRepresentation sr = new StringRepresentation(xmlRepresentation,
                MediaType.TEXT_XML);

        logger.log(Level.FINER, "Commiting data to {0}. ", url);

        Response response = this.handle(new Request(Method.POST, itemsUri, sr));
        if (response != null) {
            if (response.getStatus().isSuccess()) {
                String stringResponse = response.getEntityAsText();
                Pattern pattern = Pattern.compile("<PublishedIDs>(.*)</PublishedIDs>");
                Matcher matcher = pattern.matcher(stringResponse);

                // Check all occurance
                while (matcher.find()) {
                    String storageId = matcher.group();
                    if (storageId != null) {
                        try {
                            Integer i = new Integer(storageId);
                            retVal = storageId;
                            logger.log(Level.FINE, "Got storage-Id {0}", i);
                        } catch (NumberFormatException e) {
                            logger.log(Level.WARNING, "NumberFormatException when"
                                    + "getting storageId from response.\n{0}", e.getMessage());
                        }
                    }
                }

            }

            logger.log(Level.FINER, "RD responded with : {0}.",
                    response.getEntityAsText());
        }

        return retVal;
    }