001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.apidoc.browse;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.Path;
023import javax.ws.rs.PathParam;
024import javax.ws.rs.Produces;
025import javax.ws.rs.core.Response;
026
027import org.nuxeo.ecm.webengine.model.WebObject;
028import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
029
030@WebObject(type = "redirectWO")
031@Produces("text/html")
032public class RedirectResource extends DefaultObject {
033
034    protected String orgDistributionId = null;
035
036    protected String targetDistributionId = null;
037
038    @Override
039    protected void initialize(Object... args) {
040        orgDistributionId = (String) args[0];
041        targetDistributionId = (String) args[1];
042        targetDistributionId = targetDistributionId.replace(" ", "%20");
043    }
044
045    @GET
046    @Produces("text/html")
047    public Object get() {
048        return newLocation(targetDistributionId, null);
049    }
050
051    @GET
052    @Produces("text/html")
053    @Path("/{subPath:.*}")
054    public Object catchAll(@PathParam("subPath") String subPath) {
055        return newLocation(targetDistributionId, subPath);
056    }
057
058    protected Response newLocation(String target, String subPath) {
059        String path = getPrevious().getPath();
060        String url = ctx.getServerURL().append(path).append("/").append(target).toString();
061        if (subPath != null) {
062            url = url + "/" + subPath;
063        }
064        return redirect(url);
065    }
066}