org.apache.wink.server.internal.utils.AbstractLinksBuilderImpl Maven / Gradle / Ivy
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.wink.server.internal.utils;
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import org.apache.wink.common.RestConstants;
import org.apache.wink.common.internal.MultivaluedMapImpl;
import org.apache.wink.common.internal.uritemplate.UriTemplateProcessor;
import org.apache.wink.common.internal.utils.UriHelper;
import org.apache.wink.common.model.synd.SyndLink;
import org.apache.wink.server.handlers.MessageContext;
import org.apache.wink.server.internal.handlers.SearchResult;
import org.apache.wink.server.internal.registry.ResourceRecord;
import org.apache.wink.server.internal.registry.ResourceRegistry;
import org.apache.wink.server.utils.BaseLinksBuilder;
/**
* Base class providing common functionality for links builders.
*/
public abstract class AbstractLinksBuilderImpl implements BaseLinksBuilder {
protected Map pathParams;
protected MultivaluedMap queryParams;
protected ResourceRecord record;
protected String resourcePath;
protected String subResourcePath;
protected URI baseUri;
protected URI relativeTo;
protected MessageContext context;
protected ResourceRegistry registry;
protected boolean relativize;
protected boolean addAltParam;
protected AbstractLinksBuilderImpl(MessageContext context) {
this.context = context;
registry = context.getAttribute(ResourceRegistry.class);
pathParams = new HashMap();
queryParams = new MultivaluedMapImpl();
record = context.getAttribute(SearchResult.class).getResource().getRecord();
initPaths();
initRelativize();
initAddAltParam();
baseUri(null);
relativeTo = null;
}
private void initPaths() {
UriInfo uriInfo = context.getUriInfo();
resourcePath = null;
subResourcePath = null;
// we need to determine the path of the resource, and the path of the
// sub-resource method
// (if it exists)
List matchedURIs = uriInfo.getMatchedURIs(false);
if (matchedURIs.size() == 1) {
// if we have only one matched URI, it's a root resource without a
// sub-resource method
resourcePath = matchedURIs.get(0);
subResourcePath = null;
} else {
// we have more than one matched URI. It means we went through at
// least one sub-resource
// (locator or method).
// We compare the number of matched resources against the number of
// matched URI's to
// determine if the the invoked method is a resource method or a
// sub-resource method.
// If the number of matched URI's is the same as the number of
// matched resources, then
// we invoked a resource method.
// If the number of matched URI's is greater than the number of
// matched resources, then
// we invoked a sub-resource method.
List