mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-02 19:27:57 +08:00
Refs #1697 Desktop: Unable to updload file if app cluster is used
This commit is contained in:
parent
292f4fa048
commit
98e1d886ab
@ -31,10 +31,10 @@ public interface ClientConfig extends Config {
|
||||
String getConnectionUrl();
|
||||
|
||||
/**
|
||||
* @return Context of the middleware file download controller.<br/>
|
||||
* Usually <code>/remoting/download</code>
|
||||
* @return Context of the middleware file download controller.
|
||||
*/
|
||||
@Property("cuba.fileDownloadContext")
|
||||
@DefaultString("/download")
|
||||
String getFileDownloadContext();
|
||||
|
||||
/**
|
||||
|
@ -55,7 +55,7 @@ public class DesktopFileUploadField extends DesktopAbstractComponent<JButton> im
|
||||
fileUploading = AppBeans.get(FileUploadingAPI.NAME);
|
||||
|
||||
final JFileChooser fileChooser = new JFileChooser();
|
||||
String caption = MessageProvider.getMessage(getClass(), "selectFile");
|
||||
String caption = MessageProvider.getMessage(getClass(), "export.selectFile");
|
||||
impl = new JButton();
|
||||
impl.setAction(new AbstractAction(caption) {
|
||||
@Override
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
# Middleware connection
|
||||
cuba.connectionUrl=http://localhost:8080/cuba-core
|
||||
cuba.fileDownloadContext=/remoting/download
|
||||
|
||||
# System directories
|
||||
cuba.confDir=${cuba.desktop.home}/conf
|
||||
cuba.logDir=${cuba.desktop.home}/logs
|
||||
|
@ -8,11 +8,13 @@ package com.haulmont.cuba.gui.export;
|
||||
|
||||
import com.haulmont.cuba.client.ClientConfig;
|
||||
import com.haulmont.cuba.core.entity.FileDescriptor;
|
||||
import com.haulmont.cuba.core.global.ConfigProvider;
|
||||
import com.haulmont.cuba.core.global.FileStorageException;
|
||||
import com.haulmont.cuba.core.global.UserSessionProvider;
|
||||
import com.haulmont.cuba.core.global.*;
|
||||
import com.haulmont.cuba.core.sys.remoting.ClusterInvocationSupport;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
@ -20,6 +22,7 @@ import org.apache.http.impl.client.DefaultHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Data provider for FileDescriptor
|
||||
@ -29,16 +32,25 @@ import java.io.InputStream;
|
||||
*/
|
||||
public class FileDataProvider implements ExportDataProvider {
|
||||
|
||||
private static final int HTTP_OK = 200;
|
||||
protected Log log = LogFactory.getLog(getClass());
|
||||
|
||||
private FileDescriptor fileDescriptor;
|
||||
private InputStream inputStream;
|
||||
private boolean closed = false;
|
||||
protected FileDescriptor fileDescriptor;
|
||||
protected InputStream inputStream;
|
||||
protected boolean closed = false;
|
||||
|
||||
protected ClientConnectionManager connectionManager;
|
||||
|
||||
protected ClusterInvocationSupport clusterInvocationSupport = AppBeans.get(ClusterInvocationSupport.NAME);
|
||||
|
||||
protected UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.class);
|
||||
|
||||
protected Configuration configuration = AppBeans.get(Configuration.class);
|
||||
|
||||
protected String fileDownloadContext;
|
||||
|
||||
public FileDataProvider(FileDescriptor fileDescriptor) {
|
||||
this.fileDescriptor = fileDescriptor;
|
||||
fileDownloadContext = configuration.getConfig(ClientConfig.class).getFileDownloadContext();
|
||||
}
|
||||
|
||||
public InputStream provide() {
|
||||
@ -48,11 +60,9 @@ public class FileDataProvider implements ExportDataProvider {
|
||||
if (fileDescriptor == null)
|
||||
throw new IllegalArgumentException("Null file descriptor");
|
||||
|
||||
String fileDownloadContext = ConfigProvider.getConfig(ClientConfig.class).getFileDownloadContext();
|
||||
String connectionUrl = ConfigProvider.getConfig(ClientConfig.class).getConnectionUrl();
|
||||
|
||||
String url = connectionUrl + fileDownloadContext +
|
||||
"?s=" + UserSessionProvider.getUserSession().getId() +
|
||||
for (Iterator<String> iterator = clusterInvocationSupport.getUrlList().iterator(); iterator.hasNext(); ) {
|
||||
String url = iterator.next() + fileDownloadContext +
|
||||
"?s=" + userSessionSource.getUserSession().getId() +
|
||||
"&f=" + fileDescriptor.getId().toString();
|
||||
|
||||
HttpClient httpClient = new DefaultHttpClient();
|
||||
@ -61,24 +71,44 @@ public class FileDataProvider implements ExportDataProvider {
|
||||
try {
|
||||
HttpResponse httpResponse = httpClient.execute(httpGet);
|
||||
int httpStatus = httpResponse.getStatusLine().getStatusCode();
|
||||
switch (httpStatus) {
|
||||
case HTTP_OK:
|
||||
if (httpStatus == HttpStatus.SC_OK) {
|
||||
HttpEntity httpEntity = httpResponse.getEntity();
|
||||
if (httpEntity != null)
|
||||
if (httpEntity != null) {
|
||||
inputStream = httpEntity.getContent();
|
||||
else
|
||||
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION,
|
||||
fileDescriptor.getName());
|
||||
break;
|
||||
default:
|
||||
throw new FileStorageException(FileStorageException.Type.fromHttpStatus(httpStatus),
|
||||
fileDescriptor.getName());
|
||||
} else {
|
||||
log.debug("Unable to download file from " + url + "\nHttpEntity is null");
|
||||
if (iterator.hasNext())
|
||||
log.debug("Trying next URL");
|
||||
else
|
||||
throw new RuntimeException(
|
||||
new FileStorageException(FileStorageException.Type.IO_EXCEPTION,
|
||||
fileDescriptor.getName())
|
||||
);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} else {
|
||||
log.debug("Unable to download file from " + url + "\n" + httpResponse.getStatusLine());
|
||||
if (iterator.hasNext())
|
||||
log.debug("Trying next URL");
|
||||
else
|
||||
throw new RuntimeException(
|
||||
new FileStorageException(FileStorageException.Type.fromHttpStatus(httpStatus),
|
||||
fileDescriptor.getName())
|
||||
);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
log.debug("Unable to download file from " + url + "\n" + ex);
|
||||
if (iterator.hasNext())
|
||||
log.debug("Trying next URL");
|
||||
else
|
||||
throw new RuntimeException(
|
||||
new FileStorageException(FileStorageException.Type.IO_EXCEPTION,
|
||||
fileDescriptor.getName(), ex)
|
||||
);
|
||||
} finally {
|
||||
connectionManager = httpClient.getConnectionManager();
|
||||
}
|
||||
}
|
||||
|
||||
return inputStream;
|
||||
}
|
||||
|
@ -7,9 +7,15 @@
|
||||
package com.haulmont.cuba.gui.export;
|
||||
|
||||
import com.haulmont.cuba.client.ClientConfig;
|
||||
import com.haulmont.cuba.core.global.AppBeans;
|
||||
import com.haulmont.cuba.core.global.ConfigProvider;
|
||||
import com.haulmont.cuba.core.global.FileStorageException;
|
||||
import com.haulmont.cuba.core.sys.remoting.ClusterInvocationSupport;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
@ -17,6 +23,7 @@ import org.apache.http.impl.client.DefaultHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Class providing data using Rest API by the query given
|
||||
@ -28,12 +35,14 @@ public class RestApiDataProvider implements ExportDataProvider {
|
||||
|
||||
public static final String API_URL = "/api/";
|
||||
|
||||
private static final int HTTP_OK = 200;
|
||||
private Log log = LogFactory.getLog(getClass());
|
||||
|
||||
private ClientConnectionManager connectionManager;
|
||||
private String query;
|
||||
private InputStream inputStream;
|
||||
private boolean closed = false;
|
||||
protected ClientConnectionManager connectionManager;
|
||||
protected String query;
|
||||
protected InputStream inputStream;
|
||||
protected boolean closed = false;
|
||||
|
||||
protected ClusterInvocationSupport clusterInvocationSupport = AppBeans.get(ClusterInvocationSupport.NAME);
|
||||
|
||||
public RestApiDataProvider(String query) {
|
||||
this.query = query;
|
||||
@ -44,8 +53,11 @@ public class RestApiDataProvider implements ExportDataProvider {
|
||||
if (closed)
|
||||
throw new IllegalStateException("DataProvider is closed");
|
||||
|
||||
String connectionUrl = ConfigProvider.getConfig(ClientConfig.class).getConnectionUrl();
|
||||
String url = connectionUrl + API_URL + query;
|
||||
int remotingServletPathLen = clusterInvocationSupport.getServletPath().length() + 1;
|
||||
|
||||
for (Iterator<String> iterator = clusterInvocationSupport.getUrlList().iterator(); iterator.hasNext(); ) {
|
||||
String remotingUrl = iterator.next();
|
||||
String url = remotingUrl.substring(0, remotingUrl.length() - remotingServletPathLen) + API_URL + query;
|
||||
|
||||
HttpClient httpClient = new DefaultHttpClient();
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
@ -53,21 +65,37 @@ public class RestApiDataProvider implements ExportDataProvider {
|
||||
try {
|
||||
HttpResponse response = httpClient.execute(httpGet);
|
||||
int status = response.getStatusLine().getStatusCode();
|
||||
if (status == HTTP_OK) {
|
||||
if (status == HttpStatus.SC_OK) {
|
||||
HttpEntity httpEntity = response.getEntity();
|
||||
if (httpEntity != null)
|
||||
if (httpEntity != null) {
|
||||
inputStream = httpEntity.getContent();
|
||||
else
|
||||
throw new RuntimeException("Cannot retrieve data using Rest API and the query given: " + query);
|
||||
break;
|
||||
} else {
|
||||
throw new RuntimeException("Cannot retrieve data using Rest API and the query given: " + query
|
||||
+ ". Http status: " + status);
|
||||
log.debug("Unable to retrieve data using REST API from " + url + "\nHttpEntity is null");
|
||||
if (iterator.hasNext())
|
||||
log.debug("Trying next URL");
|
||||
else
|
||||
throw new RuntimeException("Unable to retrieve data using REST API from " + url
|
||||
+ "\nHttpEntity is null");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
} else {
|
||||
log.debug("Unable to retrieve data using REST API from " + url + "\n" + response.getStatusLine());
|
||||
if (iterator.hasNext())
|
||||
log.debug("Trying next URL");
|
||||
else
|
||||
throw new RuntimeException("Unable to retrieve data using REST API from " + url + "\n"
|
||||
+ response.getStatusLine());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.debug("Unable to retrieve data using REST API from " + url + "\n" + e);
|
||||
if (iterator.hasNext())
|
||||
log.debug("Trying next URL");
|
||||
else
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
connectionManager = httpClient.getConnectionManager();
|
||||
}
|
||||
}
|
||||
|
||||
return inputStream;
|
||||
}
|
||||
|
@ -7,11 +7,13 @@
|
||||
package com.haulmont.cuba.gui.export;
|
||||
|
||||
import com.haulmont.cuba.client.ClientConfig;
|
||||
import com.haulmont.cuba.core.global.ConfigProvider;
|
||||
import com.haulmont.cuba.core.global.FileStorageException;
|
||||
import com.haulmont.cuba.core.global.UserSessionProvider;
|
||||
import com.haulmont.cuba.core.global.*;
|
||||
import com.haulmont.cuba.core.sys.remoting.ClusterInvocationSupport;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
@ -20,6 +22,7 @@ import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* <p>$Id$</p>
|
||||
@ -29,16 +32,25 @@ import java.net.URLEncoder;
|
||||
*/
|
||||
public class SimpleFileDataProvider implements ExportDataProvider {
|
||||
|
||||
private static final int HTTP_OK = 200;
|
||||
protected Log log = LogFactory.getLog(getClass());
|
||||
|
||||
private String filePath;
|
||||
private InputStream inputStream;
|
||||
private boolean closed = false;
|
||||
protected String filePath;
|
||||
protected InputStream inputStream;
|
||||
protected boolean closed = false;
|
||||
|
||||
protected ClientConnectionManager connectionManager;
|
||||
|
||||
protected ClusterInvocationSupport clusterInvocationSupport = AppBeans.get(ClusterInvocationSupport.NAME);
|
||||
|
||||
protected UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.class);
|
||||
|
||||
protected Configuration configuration = AppBeans.get(Configuration.class);
|
||||
|
||||
protected String fileDownloadContext;
|
||||
|
||||
public SimpleFileDataProvider(String filePath) {
|
||||
this.filePath = filePath;
|
||||
fileDownloadContext = configuration.getConfig(ClientConfig.class).getFileDownloadContext();
|
||||
}
|
||||
|
||||
public InputStream provide() {
|
||||
@ -48,10 +60,9 @@ public class SimpleFileDataProvider implements ExportDataProvider {
|
||||
if (filePath == null)
|
||||
throw new IllegalArgumentException("Null file path");
|
||||
|
||||
String fileDownloadContext = ConfigProvider.getConfig(ClientConfig.class).getFileDownloadContext();
|
||||
String connectionUrl = ConfigProvider.getConfig(ClientConfig.class).getConnectionUrl();
|
||||
String url = connectionUrl + fileDownloadContext +
|
||||
"?s=" + UserSessionProvider.getUserSession().getId() +
|
||||
for (Iterator<String> iterator = clusterInvocationSupport.getUrlList().iterator(); iterator.hasNext(); ) {
|
||||
String url = iterator.next() + fileDownloadContext +
|
||||
"?s=" + userSessionSource.getUserSession().getId() +
|
||||
"&p=" + encodeUTF8(filePath);
|
||||
|
||||
HttpClient httpClient = new DefaultHttpClient();
|
||||
@ -60,22 +71,41 @@ public class SimpleFileDataProvider implements ExportDataProvider {
|
||||
try {
|
||||
HttpResponse httpResponse = httpClient.execute(httpGet);
|
||||
int httpStatus = httpResponse.getStatusLine().getStatusCode();
|
||||
switch (httpStatus) {
|
||||
case HTTP_OK:
|
||||
if (httpStatus == HttpStatus.SC_OK) {
|
||||
HttpEntity httpEntity = httpResponse.getEntity();
|
||||
if (httpEntity != null)
|
||||
if (httpEntity != null) {
|
||||
inputStream = httpEntity.getContent();
|
||||
else
|
||||
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, filePath);
|
||||
break;
|
||||
default:
|
||||
throw new FileStorageException(FileStorageException.Type.fromHttpStatus(httpStatus), filePath);
|
||||
} else {
|
||||
log.debug("Unable to download file from " + url + "\nHttpEntity is null");
|
||||
if (iterator.hasNext())
|
||||
log.debug("Trying next URL");
|
||||
else
|
||||
throw new RuntimeException(
|
||||
new FileStorageException(FileStorageException.Type.IO_EXCEPTION, filePath)
|
||||
);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} else {
|
||||
log.debug("Unable to download file from " + url + "\n" + httpResponse.getStatusLine());
|
||||
if (iterator.hasNext())
|
||||
log.debug("Trying next URL");
|
||||
else
|
||||
throw new RuntimeException(
|
||||
new FileStorageException(FileStorageException.Type.fromHttpStatus(httpStatus), filePath)
|
||||
);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
log.debug("Unable to download file from " + url + "\n" + ex);
|
||||
if (iterator.hasNext())
|
||||
log.debug("Trying next URL");
|
||||
else
|
||||
throw new RuntimeException(
|
||||
new FileStorageException(FileStorageException.Type.IO_EXCEPTION, filePath, ex)
|
||||
);
|
||||
} finally {
|
||||
connectionManager = httpClient.getConnectionManager();
|
||||
}
|
||||
}
|
||||
|
||||
return inputStream;
|
||||
}
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
# Middleware connection
|
||||
# cuba.connectionUrl=http://localhost:8080/cuba-core
|
||||
cuba.fileDownloadContext=/remoting/download
|
||||
|
||||
# Set to false if the middleware works on different JVM
|
||||
cuba.useLocalServiceInvocation=true
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
# Middleware connection
|
||||
cuba.connectionUrl=http://localhost:8080/cuba-core
|
||||
cuba.fileDownloadContext=/remoting/download
|
||||
|
||||
# Set to false if the middleware works on different JVM
|
||||
cuba.useLocalServiceInvocation=true
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user