Added support for parsing out version numbers from the URLs while constructing a callpath.

This commit is contained in:
Inderjeet Singh 2010-11-03 01:43:24 +00:00
parent 82a1a9511e
commit 40b6ddb591
2 changed files with 54 additions and 6 deletions

View File

@ -27,18 +27,30 @@ import com.google.inject.Provider;
* @author inder
*/
public final class CallPathProvider implements Provider<CallPath> {
private final HttpServletRequest request;
private final CallPath callPath;
@Inject
public CallPathProvider(HttpServletRequest request) {
this.request = request;
this(request.getPathInfo());
}
public CallPathProvider(String pathInfo) {
int index1 = pathInfo.indexOf('/');
int index2 = pathInfo.substring(index1+1).indexOf('/');
String versionStr = pathInfo.substring(index1+1, index2+1);
String callPathStr = pathInfo;
try {
// Skip over the version number from the URL
Double.parseDouble(versionStr);
callPathStr = pathInfo.substring(index2+1);
} catch (NumberFormatException e) {
// Assume that version number wasn't specified
}
this.callPath = new CallPath(callPathStr);
}
@Override
public CallPath get() {
String pathInfo = request.getPathInfo();
System.out.println("Incoming request with pathInfo: " + pathInfo);
String callPath = pathInfo;
return new CallPath(callPath);
return callPath;
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2010 Google Inc.
*
* Licensed 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 com.google.gson.wsf.inject;
import junit.framework.TestCase;
/**
* Unit test for {@link CallPathProvider}
*
* @author Inderjeet Singh
*/
public class CallPathProviderTest extends TestCase {
public void testVersionIsSkipped() {
CallPathProvider provider = new CallPathProvider("/1.0/rest/service1");
assertEquals("/rest/service1", provider.get().get());
}
public void testVersionNotPresent() {
CallPathProvider provider = new CallPathProvider("/rest/service1");
assertEquals("/rest/service1", provider.get().get());
}
}