use of org.opensourcebim.bcf.utils.FakeClosingInputStream in project BIMserver by opensourceBIM.
the class BulkUploadServlet method service.
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getHeader("Origin") != null && !getBimServer().getServerSettingsCache().isHostAllowed(request.getHeader("Origin"))) {
response.setStatus(403);
return;
}
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
String token = (String) request.getSession().getAttribute("token");
ObjectNode result = OBJECT_MAPPER.createObjectNode();
response.setContentType("text/json");
try {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
long poid = -1;
String comment = null;
if (isMultipart) {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
InputStream in = null;
String name = "";
while (iter.hasNext()) {
FileItemStream item = iter.next();
if (item.isFormField()) {
if ("token".equals(item.getFieldName())) {
token = Streams.asString(item.openStream());
} else if ("poid".equals(item.getFieldName())) {
poid = Long.parseLong(Streams.asString(item.openStream()));
} else if ("comment".equals(item.getFieldName())) {
comment = Streams.asString(item.openStream());
}
} else {
name = item.getName();
in = item.openStream();
if (poid != -1) {
ServiceInterface service = getBimServer().getServiceFactory().get(token, AccessMethod.INTERNAL).get(ServiceInterface.class);
SProject mainProject = service.getProjectByPoid(poid);
ZipInputStream zipInputStream = new ZipInputStream(in);
ZipEntry nextEntry = zipInputStream.getNextEntry();
while (nextEntry != null) {
String fullfilename = nextEntry.getName();
if (fullfilename.toLowerCase().endsWith(".ifc") || fullfilename.toLowerCase().endsWith("ifcxml") || fullfilename.toLowerCase().endsWith(".ifczip")) {
InputStreamDataSource inputStreamDataSource = new InputStreamDataSource(new FakeClosingInputStream(zipInputStream));
inputStreamDataSource.setName(name);
DataHandler ifcFile = new DataHandler(inputStreamDataSource);
if (fullfilename.contains("/")) {
String path = fullfilename.substring(0, fullfilename.lastIndexOf("/"));
String filename = fullfilename.substring(fullfilename.lastIndexOf("/") + 1);
String extension = filename.substring(filename.lastIndexOf(".") + 1);
SProject project = getOrCreatePath(service, mainProject, mainProject, path);
SDeserializerPluginConfiguration deserializer = service.getSuggestedDeserializerForExtension(extension, project.getOid());
long topicId = -1;
try {
topicId = service.checkin(project.getOid(), comment, deserializer.getOid(), -1L, filename, ifcFile, false, true);
} finally {
if (topicId != -1) {
service.cleanupLongAction(topicId);
}
}
}
} else {
if (!nextEntry.isDirectory()) {
LOGGER.info("Unknown fileextenstion " + fullfilename);
}
}
nextEntry = zipInputStream.getNextEntry();
}
// DataHandler ifcFile = new DataHandler(inputStreamDataSource);
//
// if (token != null) {
// if (topicId == -1) {
// long newTopicId = service.checkin(poid, comment, deserializerOid, -1L, name, ifcFile, merge, sync);
// result.put("topicId", newTopicId);
// } else {
// ServiceInterface service = getBimServer().getServiceFactory().get(token, AccessMethod.INTERNAL).get(ServiceInterface.class);
// long newTopicId = service.checkinInitiated(topicId, poid, comment, deserializerOid, -1L, name, ifcFile, merge, true);
// result.put("topicId", newTopicId);
// }
// }
} else {
result.put("exception", "No poid");
}
}
}
}
} catch (Exception e) {
LOGGER.error("", e);
// sendException(response, e);
return;
}
response.getWriter().write(result.toString());
}
Aggregations