Skip to content

Commit

Permalink
Corrected the webservices autowire
Browse files Browse the repository at this point in the history
  • Loading branch information
car031 committed Nov 28, 2023
1 parent 122ca16 commit f3b648c
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void setUp() throws IOException, SQLException {

createTestDirs();

context = new ClassPathXmlApplicationContext(getContexts());
context = buildApplicationContext();

createTestDatabase();
}
Expand All @@ -75,15 +75,15 @@ private void updateUserHome() {
}

/**
* Concrete implementations should return the array of context XML resources
* to use to setup the database
*
* @return array of resources(default is a single /context.xml)
* Concrete implementations should prepare and return the Spring ApplicationContext to use.
* By default it is used the ClassPathXmlApplicationContext loading the /context.xml resource.
*
* @return the ApplicationContext
*/
protected String[] getContexts() {
return new String[] { "/context.xml" };
protected ApplicationContext buildApplicationContext() {
return new ClassPathXmlApplicationContext(new String[] { "/context.xml" });
}

/**
* Concrete implementations should return the array of sql script resources
* to use to setup the database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected Map<String, File> getUploadedFiles(String sid) {
protected Session validateSession() throws InvalidSessionServerException {
return validateSession(getThreadLocalRequest());
}

private Session validateSession(HttpServletRequest request) throws InvalidSessionServerException {
try {
return ServletUtil.validateSession(request);
Expand Down Expand Up @@ -154,6 +154,27 @@ protected Session checkMenu(HttpServletRequest request, long menuId)
}
}

/**
* Check if a specific menu is accessible by the user in the current session
*
* @param menuId identifier of the menus
*
* @return the current session
*
* @throws InvalidSessionServerException the session does not exist or is
* expired
* @throws AccessDeniedException the user cannot access any menu
*/
protected Session checkMenu(long menuId) throws InvalidSessionServerException, AccessDeniedException {
try {
return ServletUtil.checkMenu(getThreadLocalRequest(), menuId);
} catch (InvalidSessionException e) {
throw new InvalidSessionServerException(e.getMessage());
} catch (ServletException e) {
throw new AccessDeniedException(e.getMessage());
}
}

protected void checkPermission(Permission permission, User user, long folderId) throws AccessDeniedException {
FolderDAO dao = (FolderDAO) Context.get().getBean(FolderDAO.class);
try {
Expand Down Expand Up @@ -418,7 +439,7 @@ protected static final Date fixDateForDB(Date date) {
date = JulianCalendarUtil.toGregorian(date);
return date;
}

/**
* Converts into Julian date in case it is before Oct 4th 1582
*
Expand Down
3 changes: 2 additions & 1 deletion logicaldoc-webapp/src/main/resources/context.properties
Original file line number Diff line number Diff line change
Expand Up @@ -856,4 +856,5 @@ zip.maxratio = 30
policy.google = https://www.logicaldoc.com/google-integration-privacy-policy

parse.email.includes=
parse.email.excludes=*
parse.email.excludes=*

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.IOException;
import java.sql.SQLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;

import com.logicaldoc.core.PersistenceException;
Expand Down Expand Up @@ -59,7 +61,7 @@ public void setUp() throws FileNotFoundException, IOException, SQLException {
} catch (ServerException e) {
throw new IOException(e);
}

Assert.assertNotNull(guiSession);
Assert.assertNotNull(SessionManager.get().get(guiSession.getSid()));
}
Expand All @@ -82,8 +84,8 @@ protected void prepareSession(String username, String password) throws ServerExc
}

@Override
protected String[] getContexts() {
return new String[] { "/contexttest.xml" };
protected ApplicationContext buildApplicationContext() {
return new ClassPathXmlApplicationContext(new String[] { "/contexttest.xml" });
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;

import org.apache.cxf.jaxrs.ext.MessageContext;
import org.apache.cxf.message.Message;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.document.AbstractDocument;
Expand Down Expand Up @@ -39,6 +39,7 @@
* @author Matteo Caruso - LogicalDOC
* @since 5.2
*/
@Component
public class AbstractService {

private static final String FOLDER = "folder ";
Expand All @@ -51,9 +52,8 @@ public void setValidateSession(boolean validateSession) {
this.validateSession = validateSession;
}

protected WebServiceContext context;

protected MessageContext messageContext;
@Autowired
protected Message currentMessage;

/**
* Utility method that validates the session and retrieve the associated
Expand Down Expand Up @@ -196,26 +196,6 @@ protected boolean isWebserviceEnabled() {
return "true".equals(Context.get().getProperties().get("webservice.enabled"));
}

public WebServiceContext getContext() {
return context;
}

public void setContext(WebServiceContext context) {
this.context = context;
}

public MessageContext getMessageContext() {
return messageContext;
}

@javax.ws.rs.core.Context
public void setMessageContext(MessageContext messageContext) {
// https://docs.oracle.com/cd/E13222_01/wls/docs92/webserv/annotations.html
// https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2790
// https://jersey.java.net/apidocs-javax.jax-rs/2.0.1/javax/ws/rs/core/Context.html
this.messageContext = messageContext;
}

/**
* Gets the current Session ID following this logic:
* <ol>
Expand All @@ -229,11 +209,7 @@ public void setMessageContext(MessageContext messageContext) {
* @return The current Session ID
*/
protected String getCurrentSessionId() {
HttpServletRequest request = null;
if (context != null && context.getMessageContext() != null)
request = (HttpServletRequest) context.getMessageContext().get(AbstractHTTPDestination.HTTP_REQUEST);
else if (messageContext != null)
request = (HttpServletRequest) messageContext.get(AbstractHTTPDestination.HTTP_REQUEST);
HttpServletRequest request = getCurrentRequest();

Session session = SessionManager.get().getSession(request);

Expand All @@ -242,6 +218,13 @@ else if (messageContext != null)
return null;
}

protected HttpServletRequest getCurrentRequest() {
HttpServletRequest request = null;
if (currentMessage != null)
request = (HttpServletRequest) currentMessage.get(AbstractHTTPDestination.HTTP_REQUEST);
return request;
}

/**
* Same as getCurrentSessionId but throws an Exception in case of bad
* session
Expand Down Expand Up @@ -274,4 +257,12 @@ public static Date convertStringToDate(String date) {
public boolean isValidateSession() {
return validateSession;
}

public Message getCurrentMessage() {
return currentMessage;
}

public void setCurrentMessage(Message currentMessage) {
this.currentMessage = currentMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.ext.MessageContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -31,33 +29,28 @@ public class RestAuthService extends SoapAuthService implements AuthService {

private static Logger log = LoggerFactory.getLogger(RestAuthService.class);

@Context
@Override
public void setMessageContext(MessageContext messageContext) {
// https://docs.oracle.com/cd/E13222_01/wls/docs92/webserv/annotations.html
// https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2790
// https://jersey.java.net/apidocs-javax.jax-rs/2.0.1/javax/ws/rs/core/Context.html
super.messageContext = messageContext;
}

@GET
@Path("/login")
@Path("/login")
@Override
public String login(@QueryParam("u") String username, @QueryParam("pw") String password) throws AuthenticationException {
public String login(@QueryParam("u")
String username, @QueryParam("pw")
String password) throws AuthenticationException {
return super.login(username, password);
}

@POST
@Path("/loginForm")
@Path("/loginForm")
@Operation(operationId = "loginForm", summary = "Login with POST", description = "Login with the credentials in a form POST")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Override
public String loginForm(@FormParam("username") String username, @FormParam("password") String password) {
public String loginForm(@FormParam("username")
String username, @FormParam("password")
String password) {
return super.login(username, password);
}

@POST
@Path("/login")
@Path("/login")
@Operation(operationId = "loginPostJSON", summary = "Login with POST in JSON format", description = "Login posting the credentials in JSON format")
@Consumes(MediaType.APPLICATION_JSON)
@Override
Expand All @@ -66,22 +59,23 @@ public String loginPostJSON(WSCredentials cred) {
}

@DELETE
@Path("/logout")
@Path("/logout")
@Override
public void logout(@QueryParam("sid") String sid) {
public void logout(@QueryParam("sid")
String sid) {
log.debug("logout({})", sid);
if (sid != null)
super.logout(sid);
}

@GET
@Path("/getSid")
@Path("/getSid")
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
@Override
public String getSid() {
return getCurrentSessionId();
}

/**
* Renews a session
*
Expand All @@ -90,10 +84,11 @@ public String getSid() {
@GET
@Path("/renew")
@Override
public void renew(@QueryParam("sid") String sid) {
public void renew(@QueryParam("sid")
String sid) {
super.renew(sid);
}

/**
* Renews the current session
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.logicaldoc.webservice.soap.endpoint;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.handler.MessageContext;

import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -24,15 +22,7 @@ public class SoapAuthService extends AbstractService implements AuthService {

@Override
public String login(String username, String password) throws AuthenticationException {
HttpServletRequest request = null;
if (context != null) {
MessageContext ctx = context.getMessageContext();
if (ctx != null)
request = (HttpServletRequest) ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
}

if (request == null)
request = messageContext.getHttpServletRequest();
HttpServletRequest request = getCurrentRequest();

Session session = SessionManager.get().newSession(username, password,
SessionManager.get().buildClient(request));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="no" default-lazy-init="false"
<beans default-lazy-init="false"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
Expand All @@ -25,6 +25,11 @@
http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<!-- This allows to access the current Message in the MessageContext of CXF -->
<bean id="currentMessage" class="org.apache.cxf.phase.PhaseInterceptorChain" factory-method="getCurrentMessage" scope="request">
<aop:scoped-proxy/>
</bean>

<bean id="WebserviceCallDAO" abstract="false" autowire="default" lazy-init="default" parent="ApplicationBaseTransactionProxy">
<property name="target">
Expand All @@ -34,7 +39,6 @@
</property>
</bean>


<bean id="GZIPInInterceptor" class="org.apache.cxf.transport.common.gzip.GZIPInInterceptor" />
<bean id="GZIPOutInterceptor" class="com.logicaldoc.webservice.ThresholdGZIPOutInterceptor">
<property name="threshold" value="${webservice.gzip}" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.logicaldoc.webservice;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

import com.logicaldoc.util.junit.AbstractTestCase;

/**
Expand All @@ -12,6 +16,15 @@
*/
public abstract class AbstractWebserviceTestCase extends AbstractTestCase {

@Override
protected ApplicationContext buildApplicationContext() {
WebserviceApplicationContext appContext = new WebserviceApplicationContext();
appContext.refresh();
return appContext;
}



@Override
protected String[] getSqlScripts() {
return new String[] { "/sql/logicaldoc-core.sql", "/sql/logicaldoc-webservice.sql", "/data.sql" };
Expand Down
Loading

0 comments on commit f3b648c

Please sign in to comment.