use of org.bimserver.longaction.LongDownloadOrCheckoutAction in project BIMserver by opensourceBIM.
the class ServiceImpl method checkout.
@Override
public Long checkout(Long roid, Long serializerOid, Boolean sync) throws ServerException, UserException {
requireAuthenticationAndRunningServer();
getAuthorization().canDownload(roid);
DatabaseSession session = getBimServer().getDatabase().createSession();
User user = null;
try {
SerializerPluginConfiguration serializerPluginConfiguration = (SerializerPluginConfiguration) session.get(serializerOid, OldQuery.getDefault());
// org.bimserver.plugins.serializers.Serializer serializer = getBimServer().getEmfSerializerFactory().get(serializerOid).createSerializer(new org.bimserver.plugins.serializers.PluginConfiguration());
if (serializerPluginConfiguration == null) {
throw new UserException("No serializer with id " + serializerOid + " could be found");
}
if (!serializerPluginConfiguration.getPluginDescriptor().getPluginClassName().equals("org.bimserver.ifc.step.serializer.IfcStepSerializerPlugin") && !serializerPluginConfiguration.getPluginDescriptor().getPluginClassName().equals("org.bimserver.ifc.xml.serializer.IfcXmlSerializerPlugin")) {
throw new UserException("Only IFC or IFCXML allowed when checking out");
}
DownloadParameters downloadParameters = new DownloadParameters(getBimServer(), DownloadType.DOWNLOAD_PROJECTS);
downloadParameters.setRoid(roid);
downloadParameters.setSerializerOid(serializerOid);
user = (User) session.get(StorePackage.eINSTANCE.getUser(), getAuthorization().getUoid(), OldQuery.getDefault());
LongDownloadOrCheckoutAction longDownloadAction = new LongCheckoutAction(getBimServer(), user.getName(), user.getUsername(), downloadParameters, getAuthorization(), getInternalAccessMethod());
try {
getBimServer().getLongActionManager().start(longDownloadAction);
} catch (CannotBeScheduledException e) {
LOGGER.error("", e);
}
if (sync) {
longDownloadAction.waitForCompletion();
}
return longDownloadAction.getProgressTopic().getKey().getId();
} catch (Exception e) {
return handleException(e);
} finally {
session.close();
}
}
use of org.bimserver.longaction.LongDownloadOrCheckoutAction in project BIMserver by opensourceBIM.
the class ServiceImpl method download.
public Long download(DownloadParameters downloadParameters, Boolean sync) throws ServerException, UserException {
requireAuthenticationAndRunningServer();
User user = null;
for (long roid : downloadParameters.getRoids()) {
getAuthorization().canDownload(roid);
}
DatabaseSession session = getBimServer().getDatabase().createSession();
try {
user = (User) session.get(StorePackage.eINSTANCE.getUser(), getAuthorization().getUoid(), OldQuery.getDefault());
} catch (BimserverDatabaseException e) {
throw new UserException(e);
} finally {
session.close();
}
LongDownloadOrCheckoutAction longDownloadAction = new LongDownloadAction(getBimServer(), user == null ? "Unknown" : user.getName(), user == null ? "Unknown" : user.getUsername(), downloadParameters, getAuthorization(), getInternalAccessMethod());
try {
getBimServer().getLongActionManager().start(longDownloadAction);
} catch (Exception e) {
LOGGER.error("", e);
}
if (sync) {
longDownloadAction.waitForCompletion();
}
return longDownloadAction.getProgressTopic().getKey().getId();
}
use of org.bimserver.longaction.LongDownloadOrCheckoutAction in project BIMserver by opensourceBIM.
the class Streamer method onText.
public void onText(Reader reader) {
JsonReader jsonreader = new JsonReader(reader);
JsonParser parser = new JsonParser();
JsonObject request = (JsonObject) parser.parse(jsonreader);
if (request.has("hb")) {
// Heartbeat, ignore
} else if (request.has("action")) {
if (request.get("action").getAsString().equals("download")) {
final long topicId = request.get("topicId").getAsLong();
Thread thread = new Thread() {
@Override
public void run() {
try {
LongAction<?> longAction = bimServer.getLongActionManager().getLongAction(topicId);
Writer writer = null;
if (longAction instanceof LongStreamingDownloadAction) {
LongStreamingDownloadAction longStreamingDownloadAction = (LongStreamingDownloadAction) longAction;
writer = longStreamingDownloadAction.getMessagingStreamingSerializer();
} else {
LongDownloadOrCheckoutAction longDownloadAction = (LongDownloadOrCheckoutAction) longAction;
writer = longDownloadAction.getMessagingSerializer();
}
boolean writeMessage = true;
int counter = 0;
long bytes = 0;
long start = System.nanoTime();
// TODO whenever a large object has been sent, the large buffer stays in memory until websocket closes...
ReusableLittleEndianDataOutputStream byteArrayOutputStream = new ReusableLittleEndianDataOutputStream();
GrowingByteBuffer growingByteBuffer = byteArrayOutputStream.getGrowingByteBuffer();
ProgressReporter progressReporter = new ProgressReporter() {
@Override
public void update(long progress, long max) {
longAction.updateProgress("test", (int) ((progress * 100) / max));
}
@Override
public void setTitle(String title) {
}
};
do {
byteArrayOutputStream.reset();
byteArrayOutputStream.writeLong(topicId);
writeMessage = writer.writeMessage(byteArrayOutputStream, progressReporter);
bytes += growingByteBuffer.usedSize();
streamingSocketInterface.sendBlocking(growingByteBuffer.array(), 0, growingByteBuffer.usedSize());
counter++;
} while (writeMessage);
long end = System.nanoTime();
LOGGER.info(counter + " messages written " + Formatters.bytesToString(bytes) + " in " + ((end - start) / 1000000) + " ms");
} catch (IOException e) {
// Probably closed/F5-ed browser
} catch (SerializerException e) {
LOGGER.error("", e);
}
}
};
thread.setName("Streamer " + topicId);
thread.start();
}
} else if (request.has("token")) {
String token = request.get("token").getAsString();
try {
ServiceMap serviceMap = bimServer.getServiceFactory().get(token, AccessMethod.JSON);
uoid = serviceMap.getBimServerAuthInterface().getLoggedInUser().getOid();
this.endpointid = bimServer.getEndPointManager().register(this);
JsonObject enpointMessage = new JsonObject();
enpointMessage.add("endpointid", new JsonPrimitive(endpointid));
streamingSocketInterface.send(enpointMessage);
} catch (UserException e) {
LOGGER.error("", e);
} catch (ServerException e) {
LOGGER.error("", e);
}
} else {
bimServer.getJsonHandler().execute(request, null, new NullWriter());
}
}
use of org.bimserver.longaction.LongDownloadOrCheckoutAction in project BIMserver by opensourceBIM.
the class ServiceImpl method triggerRevisionService.
@Override
public void triggerRevisionService(Long roid, Long soid) throws ServerException, UserException {
DatabaseSession session = getBimServer().getDatabase().createSession();
try {
Revision revision = (Revision) session.get(StorePackage.eINSTANCE.getRevision(), roid, OldQuery.getDefault());
if (revision == null) {
throw new UserException("No revision found for roid " + roid);
}
NewService newService = session.get(StorePackage.eINSTANCE.getNewService(), soid, OldQuery.getDefault());
if (revision.getServicesLinked().contains(newService)) {
// We don't want no loops
return;
}
String url = newService.getResourceUrl();
SerializerPluginConfiguration serializer = newService.getSerializer();
PackageMetaData pmd = getBimServer().getMetaDataManager().getPackageMetaData(revision.getProject().getSchema());
Query query = DefaultQueries.all(pmd);
Long topicId = download(Collections.singleton(roid), new JsonQueryObjectModelConverter(pmd).toJson(query).toString(), serializer.getOid(), false);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
LongAction<?> longAction = getBimServer().getLongActionManager().getLongAction(topicId);
if (longAction == null) {
throw new UserException("No data found for topicId " + topicId);
}
SCheckoutResult result;
if (longAction instanceof LongStreamingDownloadAction) {
LongStreamingDownloadAction longStreamingDownloadAction = (LongStreamingDownloadAction) longAction;
if (longStreamingDownloadAction.getErrors().isEmpty()) {
try {
result = longStreamingDownloadAction.getCheckoutResult();
} catch (SerializerException e) {
throw new UserException(e);
}
} else {
LOGGER.error(longStreamingDownloadAction.getErrors().get(0));
throw new ServerException(longStreamingDownloadAction.getErrors().get(0));
}
} else {
LongDownloadOrCheckoutAction longDownloadAction = (LongDownloadOrCheckoutAction) longAction;
try {
longDownloadAction.waitForCompletion();
if (longDownloadAction.getErrors().isEmpty()) {
result = longDownloadAction.getCheckoutResult();
} else {
LOGGER.error(longDownloadAction.getErrors().get(0));
throw new ServerException(longDownloadAction.getErrors().get(0));
}
} catch (Exception e) {
LOGGER.error("", e);
throw new ServerException(e);
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
LOGGER.info("Starting serialization");
DataSource datasource = result.getFile().getDataSource();
if (datasource instanceof ExtendedDataSource) {
((ExtendedDataSource) datasource).writeToOutputStream(baos, null);
}
LOGGER.info("Serialization done");
if (newService.getAccessToken() != null) {
httpPost.setHeader("Authorization", "Bearer " + newService.getAccessToken());
}
httpPost.setHeader("Input-Type", newService.getInput());
httpPost.setHeader("Output-Type", newService.getOutput());
httpPost.setEntity(new ByteArrayEntity(baos.toByteArray()));
CloseableHttpResponse response = httpclient.execute(httpPost);
LOGGER.info(response.getStatusLine().toString());
if (response.getStatusLine().getStatusCode() == 401) {
throw new UserException("Remote service responded with a 401 Unauthorized");
} else if (response.getStatusLine().getStatusCode() == 200) {
Header[] headers = response.getHeaders("Content-Disposition");
String filename = "unknown";
if (headers.length > 0) {
String contentDisposition = headers[0].getValue();
if (contentDisposition.contains("filename=")) {
int indexOf = contentDisposition.indexOf("filename=") + 10;
filename = contentDisposition.substring(indexOf, contentDisposition.indexOf("\"", indexOf + 1));
} else {
filename = contentDisposition;
}
}
Header dataTitleHeader = response.getFirstHeader("Data-Title");
String dataTitle = newService.getName() + " Results";
if (dataTitleHeader != null) {
dataTitle = dataTitleHeader.getValue();
}
byte[] responseBytes = ByteStreams.toByteArray(response.getEntity().getContent());
Action action = newService.getAction();
if (action instanceof StoreExtendedData) {
SFile file = new SFile();
file.setData(responseBytes);
file.setFilename(filename);
file.setSize(responseBytes.length);
file.setMime(response.getHeaders("Content-Type")[0].getValue());
Long fileId = uploadFile(file);
SExtendedData extendedData = new SExtendedData();
extendedData.setAdded(new Date());
extendedData.setRevisionId(roid);
extendedData.setTitle(dataTitle);
extendedData.setSize(responseBytes.length);
extendedData.setFileId(fileId);
extendedData.setSchemaId(getExtendedDataSchemaByName(newService.getOutput()).getOid());
addExtendedDataToRevision(roid, extendedData);
} else if (action instanceof CheckinRevision) {
CheckinRevision checkinRevision = (CheckinRevision) action;
Project targetProject = checkinRevision.getProject();
String extension = filename.substring(filename.lastIndexOf(".") + 1);
SDeserializerPluginConfiguration deserializer = getSuggestedDeserializerForExtension(extension, targetProject.getOid());
Long checkingTopicId = initiateCheckin(targetProject.getOid(), deserializer.getOid());
checkinInitiatedInternal(checkingTopicId, targetProject.getOid(), dataTitle, deserializer.getOid(), (long) responseBytes.length, filename, new DataHandler(new ByteArrayDataSource(responseBytes, "ifc")), false, true, newService.getOid());
}
} else {
throw new UserException("Remote service responded with a " + response.getStatusLine());
}
} catch (Exception e) {
handleException(e);
} finally {
session.close();
}
}
Aggregations