use of org.bimserver.database.queries.om.JsonQueryObjectModelConverter in project BIMserver by opensourceBIM.
the class ClientIfcModel method queryNew.
// @Override
// public void query(ObjectNode query) {
// try {
// modelState = ModelState.LOADING;
// Long downloadByTypes =
// bimServerClient.getServiceInterface().downloadByJsonQuery(Collections.singleton(roid),
// query.toString(), getJsonSerializerOid(), true);
// processDownload(downloadByTypes);
// modelState = ModelState.NONE;
// } catch (Exception e) {
// LOGGER.error("", e);
// }
// }
public void queryNew(Query query, IfcModelChangeListener ifcModelChangeListener) {
try {
modelState = ModelState.LOADING;
JsonQueryObjectModelConverter converter = new JsonQueryObjectModelConverter(getPackageMetaData());
Long topicId = bimServerClient.getServiceInterface().download(Collections.singleton(roid), converter.toJson(query).toString(), getJsonSerializerOid(), false);
waitForDonePreparing(topicId);
if (ifcModelChangeListener != null) {
addChangeListener(ifcModelChangeListener);
}
processDownload(topicId);
bimServerClient.getServiceInterface().cleanupLongAction(topicId);
if (ifcModelChangeListener != null) {
removeChangeListener(ifcModelChangeListener);
}
modelState = ModelState.NONE;
} catch (Exception e) {
LOGGER.error("", e);
}
}
use of org.bimserver.database.queries.om.JsonQueryObjectModelConverter in project BIMserver by opensourceBIM.
the class QueryObjectProvider method fromJsonNode.
public static QueryObjectProvider fromJsonNode(DatabaseSession databaseSession, BimServer bimServer, JsonNode fullQuery, Set<Long> roids, PackageMetaData packageMetaData) throws JsonParseException, JsonMappingException, IOException, QueryException {
if (fullQuery instanceof ObjectNode) {
JsonQueryObjectModelConverter converter = new JsonQueryObjectModelConverter(packageMetaData);
Query query = converter.parseJson("query", (ObjectNode) fullQuery);
return new QueryObjectProvider(databaseSession, bimServer, query, roids, packageMetaData);
} else {
throw new QueryException("Query root must be of type object");
}
}
use of org.bimserver.database.queries.om.JsonQueryObjectModelConverter in project BIMserver by opensourceBIM.
the class StreamingGeometryGenerator method generateGeometry.
@SuppressWarnings("unchecked")
public GenerateGeometryResult generateGeometry(long uoid, final DatabaseSession databaseSession, QueryContext queryContext) throws BimserverDatabaseException, GeometryGeneratingException {
GenerateGeometryResult generateGeometryResult = new GenerateGeometryResult();
packageMetaData = queryContext.getPackageMetaData();
productClass = packageMetaData.getEClass("IfcProduct");
geometryFeature = productClass.getEStructuralFeature("geometry");
representationFeature = productClass.getEStructuralFeature("Representation");
representationsFeature = packageMetaData.getEClass("IfcProductDefinitionShape").getEStructuralFeature("Representations");
itemsFeature = packageMetaData.getEClass("IfcShapeRepresentation").getEStructuralFeature("Items");
mappingSourceFeature = packageMetaData.getEClass("IfcMappedItem").getEStructuralFeature("MappingSource");
GregorianCalendar now = new GregorianCalendar();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
debugIdentifier = dateFormat.format(now.getTime());
long start = System.nanoTime();
String pluginName = "";
if (queryContext.getPackageMetaData().getSchema() == Schema.IFC4) {
pluginName = "org.bimserver.ifc.step.serializer.Ifc4StepStreamingSerializerPlugin";
} else if (queryContext.getPackageMetaData().getSchema() == Schema.IFC2X3TC1) {
pluginName = "org.bimserver.ifc.step.serializer.Ifc2x3tc1StepStreamingSerializerPlugin";
} else {
throw new GeometryGeneratingException("Unknown schema " + queryContext.getPackageMetaData().getSchema());
}
reuseGeometry = bimServer.getServerSettingsCache().getServerSettings().isReuseGeometry();
optimizeMappedItems = bimServer.getServerSettingsCache().getServerSettings().isOptimizeMappedItems();
report.setStart(new GregorianCalendar());
report.setIfcSchema(queryContext.getPackageMetaData().getSchema());
report.setMaxPerFile(maxObjectsPerFile);
report.setUseMappingOptimization(optimizeMappedItems);
report.setReuseGeometry(reuseGeometry);
try {
final StreamingSerializerPlugin ifcSerializerPlugin = (StreamingSerializerPlugin) bimServer.getPluginManager().getPlugin(pluginName, true);
if (ifcSerializerPlugin == null) {
throw new UserException("No IFC serializer found");
}
User user = (User) databaseSession.get(uoid, org.bimserver.database.OldQuery.getDefault());
UserSettings userSettings = user.getUserSettings();
report.setUserName(user.getName());
report.setUserUserName(user.getUsername());
RenderEnginePluginConfiguration renderEngine = null;
if (eoid != -1) {
renderEngine = databaseSession.get(eoid, OldQuery.getDefault());
} else {
renderEngine = userSettings.getDefaultRenderEngine();
}
if (renderEngine == null) {
throw new UserException("No default render engine has been selected for this user");
}
renderEngineName = renderEngine.getName();
int availableProcessors = Runtime.getRuntime().availableProcessors();
report.setAvailableProcessors(availableProcessors);
int maxSimultanousThreads = Math.min(bimServer.getServerSettingsCache().getServerSettings().getRenderEngineProcesses(), availableProcessors);
if (maxSimultanousThreads < 1) {
maxSimultanousThreads = 1;
}
final RenderEngineSettings settings = new RenderEngineSettings();
settings.setPrecision(Precision.SINGLE);
settings.setIndexFormat(IndexFormat.AUTO_DETECT);
settings.setGenerateNormals(true);
settings.setGenerateTriangles(true);
settings.setGenerateWireFrame(false);
final RenderEngineFilter renderEngineFilter = new RenderEngineFilter();
RenderEnginePool renderEnginePool = bimServer.getRenderEnginePools().getRenderEnginePool(packageMetaData.getSchema(), renderEngine.getPluginDescriptor().getPluginClassName(), new PluginConfiguration(renderEngine.getSettings()));
report.setRenderEngineName(renderEngine.getName());
report.setRenderEnginePluginVersion(renderEngine.getPluginDescriptor().getPluginBundleVersion().getVersion());
try (RenderEngine engine = renderEnginePool.borrowObject()) {
report.setRenderEngineVersion(engine.getVersion());
}
ThreadPoolExecutor executor = new ThreadPoolExecutor(maxSimultanousThreads, maxSimultanousThreads, 24, TimeUnit.HOURS, new ArrayBlockingQueue<Runnable>(10000000));
JsonQueryObjectModelConverter jsonQueryObjectModelConverter = new JsonQueryObjectModelConverter(packageMetaData);
String queryNameSpace = "validifc";
if (packageMetaData.getSchema() == Schema.IFC4) {
queryNameSpace = "ifc4stdlib";
}
Include objectPlacement = jsonQueryObjectModelConverter.getDefineFromFile(queryNameSpace + ":ObjectPlacement");
// TODO these are cached, so f'ing em up by doing this...
objectPlacement.makeDirectRecursive(new HashSet<>());
Set<EClass> classes = null;
if (queryContext.getOidCounters() != null) {
classes = queryContext.getOidCounters().keySet();
} else {
classes = packageMetaData.getEClasses();
}
for (EClass eClass : classes) {
if (packageMetaData.getEClass("IfcProduct").isSuperTypeOf(eClass)) {
Query query2 = new Query(eClass.getName() + "Main query", packageMetaData);
QueryPart queryPart2 = query2.createQueryPart();
queryPart2.addType(eClass, false);
Include representationInclude = queryPart2.createInclude();
representationInclude.addType(eClass, false);
representationInclude.addFieldDirect("Representation");
Include representationsInclude = representationInclude.createInclude();
representationsInclude.addType(packageMetaData.getEClass("IfcProductRepresentation"), true);
representationsInclude.addFieldDirect("Representations");
Include itemsInclude = representationsInclude.createInclude();
itemsInclude.addType(packageMetaData.getEClass("IfcShapeRepresentation"), false);
itemsInclude.addFieldDirect("Items");
Include mappingSourceInclude = itemsInclude.createInclude();
mappingSourceInclude.addType(packageMetaData.getEClass("IfcMappedItem"), false);
mappingSourceInclude.addFieldDirect("MappingSource");
mappingSourceInclude.addFieldDirect("MappingTarget");
Include representationMap = mappingSourceInclude.createInclude();
representationMap.addType(packageMetaData.getEClass("IfcRepresentationMap"), false);
Include targetInclude = mappingSourceInclude.createInclude();
targetInclude.addType(packageMetaData.getEClass("IfcCartesianTransformationOperator3D"), false);
targetInclude.addFieldDirect("Axis1");
targetInclude.addFieldDirect("Axis2");
targetInclude.addFieldDirect("Axis3");
targetInclude.addFieldDirect("LocalOrigin");
queryPart2.addInclude(objectPlacement);
Map<Long, Map<Long, ProductDef>> representationMapToProduct = new HashMap<>();
QueryObjectProvider queryObjectProvider2 = new QueryObjectProvider(databaseSession, bimServer, query2, Collections.singleton(queryContext.getRoid()), packageMetaData);
HashMapVirtualObject next = queryObjectProvider2.next();
while (next != null) {
if (next.eClass() == eClass) {
HashMapVirtualObject representation = next.getDirectFeature(representationFeature);
if (representation != null) {
List<HashMapVirtualObject> representations = representation.getDirectListFeature(representationsFeature);
if (representations != null) {
for (HashMapVirtualObject representationItem : representations) {
String representationIdentifier = (String) representationItem.get("RepresentationIdentifier");
if (representationIdentifier.equals("Body") || representationIdentifier.equals("Facetation")) {
List<HashMapVirtualObject> items = representationItem.getDirectListFeature(itemsFeature);
for (HashMapVirtualObject item : items) {
report.addRepresentationItem(item.eClass().getName());
HashMapVirtualObject mappingTarget = item.getDirectFeature(packageMetaData.getEReference("IfcMappedItem", "MappingTarget"));
double[] mappingMatrix = Matrix.identity();
double[] productMatrix = Matrix.identity();
if (mappingTarget != null) {
HashMapVirtualObject axis1 = mappingTarget.getDirectFeature(packageMetaData.getEReference("IfcCartesianTransformationOperator", "Axis1"));
HashMapVirtualObject axis2 = mappingTarget.getDirectFeature(packageMetaData.getEReference("IfcCartesianTransformationOperator", "Axis2"));
HashMapVirtualObject axis3 = mappingTarget.getDirectFeature(packageMetaData.getEReference("IfcCartesianTransformationOperator", "Axis3"));
HashMapVirtualObject localOrigin = mappingTarget.getDirectFeature(packageMetaData.getEReference("IfcCartesianTransformationOperator", "LocalOrigin"));
double[] a1 = null;
double[] a2 = null;
double[] a3 = null;
if (axis3 != null) {
List<Double> list = (List<Double>) axis3.get("DirectionRatios");
a3 = new double[] { list.get(0), list.get(1), list.get(2) };
} else {
a3 = new double[] { 0, 0, 1, 1 };
Vector.normalize(a3);
}
if (axis1 != null) {
List<Double> list = (List<Double>) axis1.get("DirectionRatios");
a1 = new double[] { list.get(0), list.get(1), list.get(2) };
Vector.normalize(a1);
} else {
// if (a3[0] == 1 && a3[1] == 0 && a3[2] == 0) {
a1 = new double[] { 1, 0, 0, 1 };
// } else {
// a1 = new double[]{0, 1, 0, 1};
// }
}
double[] xVec = Vector.scalarProduct(Vector.dot(a1, a3), a3);
double[] xAxis = Vector.subtract(a1, xVec);
Vector.normalize(xAxis);
if (axis2 != null) {
List<Double> list = (List<Double>) axis2.get("DirectionRatios");
a2 = new double[] { list.get(0), list.get(1), list.get(2) };
Vector.normalize(a2);
} else {
a2 = new double[] { 0, 1, 0, 1 };
}
double[] tmp = Vector.scalarProduct(Vector.dot(a2, a3), a3);
double[] yAxis = Vector.subtract(a2, tmp);
tmp = Vector.scalarProduct(Vector.dot(a2, xAxis), xAxis);
yAxis = Vector.subtract(yAxis, tmp);
Vector.normalize(yAxis);
a2 = yAxis;
a1 = xAxis;
List<Double> t = (List<Double>) localOrigin.get("Coordinates");
mappingMatrix = new double[] { a1[0], a1[1], a1[2], 0, a2[0], a2[1], a2[2], 0, a3[0], a3[1], a3[2], 0, t.get(0).doubleValue(), t.get(1).doubleValue(), t.get(2).doubleValue(), 1 };
}
HashMapVirtualObject placement = next.getDirectFeature(packageMetaData.getEReference("IfcProduct", "ObjectPlacement"));
if (placement != null) {
productMatrix = placementToMatrix(placement);
}
HashMapVirtualObject mappingSource = item.getDirectFeature(mappingSourceFeature);
if (mappingSource != null) {
Map<Long, ProductDef> map = representationMapToProduct.get(mappingSource.getOid());
if (map == null) {
map = new LinkedHashMap<>();
representationMapToProduct.put(mappingSource.getOid(), map);
}
ProductDef pd = new ProductDef(next.getOid());
pd.setObject(next);
pd.setProductMatrix(productMatrix);
pd.setMappingMatrix(mappingMatrix);
map.put(next.getOid(), pd);
}
}
}
}
}
}
}
next = queryObjectProvider2.next();
}
Set<Long> done = new HashSet<>();
for (Long repMapId : representationMapToProduct.keySet()) {
Map<Long, ProductDef> map = representationMapToProduct.get(repMapId);
if (map.size() > 1) {
Query query = new Query("Reuse query " + eClass.getName(), packageMetaData);
QueryPart queryPart = query.createQueryPart();
queryPart.addType(eClass, false);
long masterOid = map.values().iterator().next().getOid();
for (ProductDef pd : map.values()) {
done.add(pd.getOid());
if (!optimizeMappedItems) {
queryPart.addOid(pd.getOid());
} else {
pd.setMasterOid(masterOid);
}
}
if (optimizeMappedItems) {
queryPart.addOid(masterOid);
}
LOGGER.debug("Running " + map.size() + " objects in one batch because of reused geometry " + (eClass.getName()));
processX(databaseSession, queryContext, generateGeometryResult, ifcSerializerPlugin, settings, renderEngineFilter, renderEnginePool, executor, eClass, query, queryPart, true, map, map.size());
}
}
Query query3 = new Query("Remaining " + eClass.getName(), packageMetaData);
QueryPart queryPart3 = query3.createQueryPart();
queryPart3.addType(eClass, false);
Include include3 = queryPart3.createInclude();
include3.addType(eClass, false);
include3.addFieldDirect("Representation");
Include rInclude = include3.createInclude();
rInclude.addType(packageMetaData.getEClass("IfcProductRepresentation"), false);
rInclude.addFieldDirect("Representations");
Include representationsInclude2 = rInclude.createInclude();
representationsInclude2.addType(packageMetaData.getEClass("IfcShapeModel"), false);
queryObjectProvider2 = new QueryObjectProvider(databaseSession, bimServer, query3, Collections.singleton(queryContext.getRoid()), packageMetaData);
next = queryObjectProvider2.next();
while (next != null) {
if (next.eClass() == eClass && !done.contains(next.getOid())) {
HashMapVirtualObject representation = next.getDirectFeature(representationFeature);
if (representation != null) {
List<HashMapVirtualObject> list = representation.getDirectListFeature(packageMetaData.getEReference("IfcProductRepresentation", "Representations"));
boolean goForIt = goForIt(list);
if (goForIt) {
Query query = new Query("Main " + eClass.getName(), packageMetaData);
QueryPart queryPart = query.createQueryPart();
queryPart.addType(eClass, false);
int x = 1;
queryPart.addOid(next.getOid());
while (next != null && x < maxObjectsPerFile) {
next = queryObjectProvider2.next();
if (next != null) {
if (next.eClass() == eClass && !done.contains(next.getOid())) {
representation = next.getDirectFeature(representationFeature);
if (representation != null) {
list = representation.getDirectListFeature(packageMetaData.getEReference("IfcProductRepresentation", "Representations"));
boolean goForIt2 = goForIt(list);
if (goForIt2) {
queryPart.addOid(next.getOid());
x++;
}
}
}
}
}
processX(databaseSession, queryContext, generateGeometryResult, ifcSerializerPlugin, settings, renderEngineFilter, renderEnginePool, executor, eClass, query, queryPart, false, null, x);
}
}
}
next = queryObjectProvider2.next();
}
}
}
// for (Long l : counters.keySet()) {
// LOGGER.info(databaseSession.getEClassForOid(l).getName() + "(" + l + "): " + counters.get(l));
// }
allJobsPushed = true;
executor.shutdown();
executor.awaitTermination(24, TimeUnit.HOURS);
long end = System.nanoTime();
long total = totalBytes.get() - (bytesSavedByHash.get() + bytesSavedByTransformation.get() + bytesSavedByMapping.get());
LOGGER.info("Rendertime: " + Formatters.nanosToString(end - start) + ", " + "Reused (by hash): " + Formatters.bytesToString(bytesSavedByHash.get()) + ", Reused (by transformation): " + Formatters.bytesToString(bytesSavedByTransformation.get()) + ", Reused (by mapping): " + Formatters.bytesToString(bytesSavedByMapping.get()) + ", Total: " + Formatters.bytesToString(totalBytes.get()) + ", Final: " + Formatters.bytesToString(total));
} catch (Exception e) {
running = false;
LOGGER.error("", e);
report.setEnd(new GregorianCalendar());
throw new GeometryGeneratingException(e);
}
report.setEnd(new GregorianCalendar());
try {
writeDebugFile();
} catch (IOException e) {
LOGGER.debug("", e);
}
return generateGeometryResult;
}
use of org.bimserver.database.queries.om.JsonQueryObjectModelConverter in project BIMserver by opensourceBIM.
the class StreamingGeometryGenerator method processX.
private void processX(final DatabaseSession databaseSession, QueryContext queryContext, GenerateGeometryResult generateGeometryResult, final StreamingSerializerPlugin ifcSerializerPlugin, final RenderEngineSettings settings, final RenderEngineFilter renderEngineFilter, RenderEnginePool renderEnginePool, ThreadPoolExecutor executor, EClass eClass, Query query, QueryPart queryPart, boolean geometryReused, Map<Long, ProductDef> map, int nrObjects) throws QueryException, IOException {
JsonQueryObjectModelConverter jsonQueryObjectModelConverter = new JsonQueryObjectModelConverter(packageMetaData);
String queryNameSpace = "validifc";
if (packageMetaData.getSchema() == Schema.IFC4) {
queryNameSpace = "ifc4stdlib";
}
if (eClass.getName().equals("IfcAnnotation")) {
// IfcAnnotation also has the field ContainedInStructure, but that is it's own field (looks like a hack on the IFC-spec side)
queryPart.addInclude(jsonQueryObjectModelConverter.getDefineFromFile(queryNameSpace + ":IfcAnnotationContainedInStructure"));
} else {
queryPart.addInclude(jsonQueryObjectModelConverter.getDefineFromFile(queryNameSpace + ":ContainedInStructure"));
}
if (packageMetaData.getSchema() == Schema.IFC4) {
queryPart.addInclude(jsonQueryObjectModelConverter.getDefineFromFile(queryNameSpace + ":IsTypedBy"));
}
queryPart.addInclude(jsonQueryObjectModelConverter.getDefineFromFile(queryNameSpace + ":Decomposes"));
queryPart.addInclude(jsonQueryObjectModelConverter.getDefineFromFile(queryNameSpace + ":OwnerHistory"));
Include representationInclude = jsonQueryObjectModelConverter.getDefineFromFile(queryNameSpace + ":Representation");
queryPart.addInclude(representationInclude);
Include objectPlacement = jsonQueryObjectModelConverter.getDefineFromFile(queryNameSpace + ":ObjectPlacement");
queryPart.addInclude(objectPlacement);
if (packageMetaData.getEClass("IfcElement").isSuperTypeOf(eClass)) {
Include openingsInclude = queryPart.createInclude();
openingsInclude.addType(packageMetaData.getEClass(eClass.getName()), false);
openingsInclude.addField("HasOpenings");
Include hasOpenings = openingsInclude.createInclude();
hasOpenings.addType(packageMetaData.getEClass("IfcRelVoidsElement"), false);
hasOpenings.addField("RelatedOpeningElement");
hasOpenings.addInclude(representationInclude);
hasOpenings.addInclude(objectPlacement);
// Include relatedOpeningElement = hasOpenings.createInclude();
// relatedOpeningElement.addType(packageMetaData.getEClass("IfcOpeningElement"), false);
// relatedOpeningElement.addField("HasFillings");
// Include hasFillings = relatedOpeningElement.createInclude();
// hasFillings.addType(packageMetaData.getEClass("IfcRelFillsElement"), false);
// hasFillings.addField("RelatedBuildingElement");
}
QueryObjectProvider queryObjectProvider = new QueryObjectProvider(databaseSession, bimServer, query, Collections.singleton(queryContext.getRoid()), packageMetaData);
ReportJob job = report.newJob(eClass.getName(), nrObjects);
GeometryRunner runner = new GeometryRunner(this, eClass, renderEnginePool, databaseSession, settings, queryObjectProvider, ifcSerializerPlugin, renderEngineFilter, generateGeometryResult, queryContext, query, geometryReused, map, job, reuseGeometry);
executor.submit(runner);
jobsTotal.incrementAndGet();
}
use of org.bimserver.database.queries.om.JsonQueryObjectModelConverter 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