3

Here's the basics of what I'm trying to do. I'm working on a project where I'm creating a mashup page from various web APIs. The mashup page contains country information.

I have a BLL layer where I create the mashup page (html page with country input using Chunk Library). This is the method used to set up the template

public String GetTemplate(Country country) throws IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL resource = classLoader.getResource("BLL/themes/page.html");
    Theme theme = new Theme("src/BLL", "themes");

    Chunk html = theme.makeChunk("page", "html");

    html.set("countryName", country.getName());
    html.set("countryCode", country.getCode());
    html.set("countryContinent", country.getContinent());
    html.set("countryCapital", country.getCapital());
    html.set("countryAreaInSqKm", country.getAreaInSqKm());
    html.set("countryPopulation", country.getPopulation());
    html.set("countryCurrencyCode", country.getCurrencyCode());
    html.set("countryExchangeRate", country.getExchangeRate());
    html.set("countryFlagUrl", country.getFlagUrl());
    html.set("countryDescription", country.getDescription());
    html.set("countryMap", country.getMapLocation());

    return html.toString();
}

Eventually I'll be using a database for all available countries.

So what I want to do on the Restlet server is iterate through all countries, like so, or in a similar manner?

public Restlet createInboundRoot() {
    Router router = new Router(getContext());
    router.attach("/countries/", Countries.class);
    for (Country country : cm.GetAllCountries()) {
         router.attach("/countries/" + country.getName(), new CountryTemplate(country)); 
         // Obviously this doesn't work, and it doesn't work with getClass() either.
    }

    return router;
}

As such I'm trying to get my resource to return the html in the get method (from the TemplateManager):

public class CountryTemplate extends ServerResource {
    CountryManager cm = null;
    TemplateManager tm = null;
    Country country = null;

    public CountryTemplate(Country _country) throws Exception {
        cm = new CountryManager();
        tm = new TemplateManager();
        country = _country;
    }

    @Get("html")
    public String getTemplate() throws IOException, RemoteException, UnsupportedCurrencyException {
        return tm.GetTemplate(country);
    }
}

Is there indeed a way to do it like this, or am I approaching this all wrong?

If anyone else should find themselves here, Tim's code worked exactly as required. The posted code was altered to his version.

4

1 回答 1

3

与其尝试对 REST 资源的动态动态行为进行硬编码,不如使用查询参数。

首先,您可以创建一个新的 REST 资源/countries/html/并将其绑定到 class CountryResource。此资源将接受countryID与您要访问其 HTML 的国家/地区对应的查询参数:

public Restlet createInboundRoot() {
    Router router = new Router(getContext());
    router.attach("/countries/", Countries.class);
    router.attach("/countries/html/", CountryResource.class); 

    return router;
}

在您的定义中CountryResource,您可以访问查询参数countryID以获取相应的 HTML 内容:

public class CountryResource extends ServerResource {
    CountryManager cm;
    TemplateManager tm;

    public CountryTemplate() throws Exception {
        // Note: I usually do NOT define a constructor using Restlets,
        // but you should be OK doing this
        cm = new CountryManager();
        tm = new TemplateManager();
    }

    @Get("html")
    public String getTemplate() throws IOException, RemoteException, UnsupportedCurrencyException {
        String countryID = getQueryValue("countryID");
        Country country = tm.getCountry(countryID);

        return tm.GetTemplate(country);
    }
}

我假设CountryManager有一个方法getCountry()可以Country根据输入字符串返回一个对象。如果当前不存在这样的方法,那么您将需要找到一种方法将传入的查询参数映射到Country代码中的实际对象。

于 2015-05-17T14:35:01.370 回答