Jan 16, 2006

[Library] Restlet にチャレンジ (2)

サンプルコード(1)

まずはリクエストの内容を表示するサンプル。

package jp.in_vitro.codelets.restletsample;

import org.restlet.AbstractRestlet;
import org.restlet.Restlet;
import org.restlet.RestletCall;
import org.restlet.RestletException;
import org.restlet.component.DefaultRestletContainer;
import org.restlet.component.RestletContainer;
import org.restlet.data.MediaTypes;
import org.restlet.data.Preference;

import com.noelios.restlet.data.StringRepresentation;
import com.noelios.restlet.ext.jetty.JettyServer;

public class RestletContainerSample {

    public static void main(String[] args) throws Exception {
        RestletContainerSample me = new RestletContainerSample();
        me.initialize();
        me.execute();
    }

    public RestletContainerSample() {
        super();
    }

    protected void initialize() {
        // Restlet API に Restlet 実装を登録。
        com.noelios.restlet.Engine.register();
    }

    protected void execute() throws Exception {
        // Jetty と Restlet コンテナの準備
        RestletContainer restletContainer = new DefaultRestletContainer(
                "My container");
        JettyServer httpServer = new JettyServer("Web Server", 8080,
                restletContainer);
        restletContainer.addServer(httpServer);

        // Restlet コンテナに Restlet を登録して、Restlet コンテナを開始。
        Restlet myRestlet = new MyRestlet(restletContainer);
        restletContainer.attach("http://localhost:8080/restlet", myRestlet);
        restletContainer.start();
    }

    protected static class MyRestlet extends AbstractRestlet {

        public MyRestlet(final RestletContainer restletContainer) {
            super(restletContainer);
        }

        public void handle(final RestletCall call) throws RestletException {
            StringBuffer message = new StringBuffer();
            message.append("<html><body>");
            message.append("<dl>");
            message.append("<dt>Client Address</dd>");
            message.append("<dd>" + call.getClientAddress() + "</dd>");
            message.append("<dt>Client Name</dd>");
            message.append("<dd>" + call.getClientName() + "</dd>");
            message.append("<dt>Method</dd>");
            message.append("<dd>" + call.getMethod() + "</dd>");
            if (call.getReferrerRef() != null) {
                message.append("<dt>Referrer Ref</dd>");
                message.append("<dd>" + call.getReferrerRef().getPath()
                        + "</dd>");
            }
            if (call.getResourceRef() != null) {
                message.append("<dt>Resource Ref</dd>");
                message.append("<dd>" + call.getResourceRef().getPath()
                        + "</dd>");
            }
            if (call.getSecurity() != null) {
                message.append("<dt>Security (description)</dd>");
                message.append("<dd>" + call.getSecurity().getDescription()
                        + "</dd>");
                message.append("<dt>Security (challenge request)</dd>");
                message.append("<dd>"
                        + call.getSecurity().getChallengeRequest() + "</dd>");
                message.append("<dt>Security (challenge response)</dd>");
                message.append("<dd>"
                        + call.getSecurity().getChallengeResponse() + "</dd>");
            }
            if (call.getStatus() != null) {
                message.append("<dt>Status</dd>");
                message.append("<dd>" + call.getStatus() + "</dd>");
            }
            if (call.getMatches() != null) {
                message.append("<dt>Matches</dd>");
                message.append("<dd>");
                for (String match : call.getMatches()) {
                    message.append(match);
                    message.append(",");
                }
                message.append("</dd>");
            }
            if (call.getPaths() != null) {
                message.append("<dt>Paths</dd>");
                message.append("<dd>");
                for (String path : call.getPaths()) {
                    message.append(path);
                    message.append("<br>");
                }
                message.append("</dd>");
            }
            if (call.getMediaTypePrefs() != null) {
                message.append("<dt>Media Type Prefs</dd>");
                message.append("<dd>");
                for (Preference preference : call.getMediaTypePrefs()) {
                    message.append(preference.getMetadata());
                    message.append(",");
                }
                message.append("</dd>");
            }
            if (call.getLanguagePrefs() != null) {
                message.append("<dt>Language Prefs</dd>");
                message.append("<dd>");
                for (Preference preference : call.getLanguagePrefs()) {
                    message.append(preference.getMetadata());
                    message.append(",");
                }
                message.append("</dd>");
            }
            if (call.getCharacterSetPrefs() != null) {
                message.append("<dt>Character Set Prefs</dd>");
                message.append("<dd>");
                for (Preference preference : call.getCharacterSetPrefs()) {
                    message.append(preference.getMetadata());
                    message.append(",");
                }
                message.append("</dd>");
            }
            if (call.getConditions() != null) {
                message.append("<dt>Conditions (description)</dd>");
                message.append("<dd>" + call.getConditions().getDescription()
                        + "</dd>");
                message.append("<dt>Conditions (modified since)</dd>");
                message.append("<dd>" + call.getConditions().getModifiedSince()
                        + "</dd>");
                message.append("<dt>Conditions (unmodified since)</dd>");
                message.append("<dd>"
                        + call.getConditions().getUnmodifiedSince() + "</dd>");
            }
            message.append("</dl>");
            message.append("</body></html>");
            call.setOutput(new StringRepresentation(new String(message),
                    MediaTypes.TEXT_HTML));
        }
    }
}

サンプルコード(2)

URL に従ってファイルを返すサンプル。

package jp.in_vitro.codelets.restletsample;

import org.restlet.component.DefaultRestletContainer;
import org.restlet.component.RestletContainer;
import org.restlet.data.MediaTypes;

import com.noelios.restlet.DirectoryRestlet;
import com.noelios.restlet.ext.jetty.JettyServer;

public class DirectoryRestletSample {

    public static void main(final String[] args) throws Exception {
        DirectoryRestletSample me = new DirectoryRestletSample();
        me.initialize();
        me.execute();
    }

    public DirectoryRestletSample() {
        super();
    }

    protected void initialize() {
        // Restlet API に Restlet 実装を登録。
        com.noelios.restlet.Engine.register();
    }

    protected void execute() throws Exception {
        // Jetty と Restlet コンテナの準備
        RestletContainer restletContainer = new DefaultRestletContainer(
                "My container");
        JettyServer httpServer = new JettyServer("Web Server", 8080,
                restletContainer);
        restletContainer.addServer(httpServer);

        // リクエストのパスに従ってファイルを返す Restlet
        DirectoryRestlet dirRestlet = new DirectoryRestlet(restletContainer,
                "c:/tmp", true, "index");
        dirRestlet.addExtension("txt", MediaTypes.TEXT_PLAIN);

        // Restlet コンテナに Restlet を登録して、Restlet コンテナを開始。
        restletContainer.attach("http://localhost:8080/", dirRestlet);
        restletContainer.start();
    }
}

Posted in Library | このエントリーをはてなブックマークに追加 | この記事をクリップ! livedoor クリップ |