diff options
author | Olivier Lamy <olamy@apache.org> | 2011-11-23 17:15:42 +0000 |
---|---|---|
committer | Olivier Lamy <olamy@apache.org> | 2011-11-23 17:15:42 +0000 |
commit | 16f3847d18877a2f59c13fea6e05ad9ee0ce5a4b (patch) | |
tree | 6a7d2bf8f1fb3d0b7179d4f12bc14f56be6cecf6 | |
parent | 0884c078c54b70754e412eade3f5eee1300afa1b (diff) | |
download | archiva-16f3847d18877a2f59c13fea6e05ad9ee0ce5a4b.tar.gz archiva-16f3847d18877a2f59c13fea6e05ad9ee0ce5a4b.zip |
simplify with using @Context annotation from jaxrs to get HttpServletRequest in rest services
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1205494 13f79535-47bb-0310-9956-ffa450edef68
6 files changed, 9 insertions, 222 deletions
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java index 9ba9014e8..dbb291a30 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java @@ -48,6 +48,7 @@ import org.springframework.stereotype.Service; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.Context; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -68,6 +69,9 @@ public class DefaultSearchService @Inject private UserRepositories userRepositories; + @Context + private HttpServletRequest httpServletRequest; + public List<Artifact> quickSearch( String queryString ) throws ArchivaRestServiceException { @@ -216,7 +220,6 @@ public class DefaultSearchService protected List<Artifact> getArtifacts( SearchResults searchResults ) { - HttpContext httpContext = HttpContextThreadLocal.get(); if ( searchResults == null || searchResults.isEmpty() ) { return Collections.emptyList(); @@ -235,7 +238,7 @@ public class DefaultSearchService if ( StringUtils.isNotBlank( version ) ) { versionned.setVersion( version ); - versionned.setUrl( getArtifactUrl( httpContext, versionned ) ); + versionned.setUrl( getArtifactUrl( versionned ) ); artifacts.add( versionned ); @@ -248,17 +251,13 @@ public class DefaultSearchService /** * TODO add a configuration mechanism to have configured the base archiva url - * @param httpContext * @param artifact * @return */ - private String getArtifactUrl( HttpContext httpContext, Artifact artifact ) + private String getArtifactUrl( Artifact artifact ) { - if ( httpContext == null ) - { - return null; - } - if ( httpContext.getHttpServletRequest() == null ) + + if ( httpServletRequest == null ) { return null; } @@ -266,7 +265,7 @@ public class DefaultSearchService { return null; } - StringBuilder sb = new StringBuilder( getBaseUrl( httpContext.getHttpServletRequest() ) ); + StringBuilder sb = new StringBuilder( getBaseUrl( httpServletRequest ) ); sb.append( "/repository" ); if ( !StringUtils.startsWith( artifact.getUrl(), "/" ) ) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContext.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContext.java deleted file mode 100644 index 84b69ff7f..000000000 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContext.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.apache.archiva.rest.services.interceptors; -/* - * 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. - */ - -import javax.servlet.http.HttpServletRequest; -import java.io.Serializable; - -/** - * @author Olivier Lamy - * @since 1.4-M1 - */ -public class HttpContext - implements Serializable -{ - private HttpServletRequest httpServletRequest; - - public HttpContext() - { - // no op - } - - public HttpServletRequest getHttpServletRequest() - { - return httpServletRequest; - } - - public HttpContext setHttpServletRequest( HttpServletRequest httpServletRequest ) - { - this.httpServletRequest = httpServletRequest; - return this; - } -} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContextThreadLocal.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContextThreadLocal.java deleted file mode 100644 index 8ec1cab1d..000000000 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContextThreadLocal.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.apache.archiva.rest.services.interceptors; -/* - * 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. - */ - -/** - * @author Olivier Lamy - * @since 1.4-M1 - */ -public class HttpContextThreadLocal -{ - private static final ThreadLocal<HttpContext> userThreadLocal = new ThreadLocal<HttpContext>(); - - public static void set( HttpContext httpContext ) - { - userThreadLocal.set( httpContext ); - } - - public static HttpContext get() - { - return userThreadLocal.get(); - } -} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContextThreadLocalCleaner.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContextThreadLocalCleaner.java deleted file mode 100644 index 517cccaf5..000000000 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContextThreadLocalCleaner.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.apache.archiva.rest.services.interceptors; -/* - * 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. - */ - -import org.apache.cxf.interceptor.Fault; -import org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor; -import org.apache.cxf.jaxrs.model.OperationResourceInfo; -import org.apache.cxf.message.Message; -import org.apache.cxf.phase.AbstractPhaseInterceptor; -import org.apache.cxf.phase.Phase; -import org.apache.cxf.phase.PhaseInterceptor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Service; - -import javax.ws.rs.core.Response; - -/** - * @author Olivier Lamy - * @since 1.4-M1 - */ -@Service( "httpContextThreadLocalCleaner#rest" ) -public class HttpContextThreadLocalCleaner - extends AbstractPhaseInterceptor<Message> - implements PhaseInterceptor<Message> -{ - private Logger log = LoggerFactory.getLogger( getClass() ); - - public HttpContextThreadLocalCleaner( String phase ) - { - super( phase ); - addAfter( JAXRSInInterceptor.class.getName() ); - } - - - public HttpContextThreadLocalCleaner() - { - super( Phase.PRE_STREAM ); - addAfter( JAXRSInInterceptor.class.getName() ); - } - - - public Response handleResponse( Message message, OperationResourceInfo operationResourceInfo, Response response ) - { - log.debug( "handleResponse" ); - cleanup(); - return null; - } - - private void cleanup() - { - HttpContextThreadLocal.set( null ); - } - - public void handleMessage( Message message ) - throws Fault - { - log.debug( "handleMessage" ); - cleanup(); - } -} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContextThreadLocalInterceptor.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContextThreadLocalInterceptor.java deleted file mode 100644 index 4a879ac23..000000000 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/interceptors/HttpContextThreadLocalInterceptor.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.apache.archiva.rest.services.interceptors; -/* - * 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. - */ - -import org.apache.cxf.jaxrs.ext.RequestHandler; -import org.apache.cxf.jaxrs.model.ClassResourceInfo; -import org.apache.cxf.message.Message; -import org.springframework.stereotype.Service; - -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.core.Response; - -/** - * @author Olivier Lamy - */ -@Service( "httpContextThreadLocalInterceptor#rest" ) -public class HttpContextThreadLocalInterceptor - implements RequestHandler -{ - public Response handleRequest( Message m, ClassResourceInfo resourceClass ) - { - HttpContextThreadLocal.set( new HttpContext().setHttpServletRequest( getHttpServletRequest( m ) ) ); - return null; - } - - public HttpServletRequest getHttpServletRequest( Message message ) - { - // FIXME use a constant from cxf - return (HttpServletRequest) message.get( "HTTP.REQUEST" ); - } -} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml index f5cfcb7d5..5cd75664d 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml @@ -44,7 +44,6 @@ <jaxrs:providers> <ref bean="authenticationInterceptor#rest"/> <ref bean="permissionInterceptor#rest"/> - <ref bean="httpContextThreadLocalInterceptor#rest"/> <ref bean="archivaRestServiceExceptionMapper"/> </jaxrs:providers> @@ -62,7 +61,6 @@ <jaxrs:outInterceptors> <ref bean="threadLocalUserCleaner#rest"/> - <ref bean="httpContextThreadLocalCleaner#rest"/> </jaxrs:outInterceptors> <jaxrs:outFaultInterceptors> |