Ejemplo n.º 1
0
 /*
  * This will create a project and checkin the AC11 IFC file
  */
 @Test
 public void testUpload() {
   try {
     ServiceMap serviceMap = bimServer.getServiceFactory().get(AccessMethod.INTERNAL);
     ServiceInterface service = serviceMap.get(ServiceInterface.class);
     serviceMap.get(AuthInterface.class).login(username, password);
     SProject project =
         serviceMap.getServiceInterface().addProject("test " + new Random().nextInt(), "ifc4");
     Path sourceFile = TestFile.AC11.getFile();
     service.checkin(
         project.getOid(),
         "test",
         -1L,
         Files.size(sourceFile),
         "test",
         new DataHandler(new FileDataSource(sourceFile.toFile())),
         false,
         true); // TODO
   } catch (ServiceException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (PublicInterfaceNotFoundException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 2
0
 /*
  * This will list all projects, select the first project with at least one
  * revision and dump the names of all walls within the last revision of that
  * project
  */
 @Test
 public void testDump() {
   try {
     ServiceMap serviceMap = bimServer.getServiceFactory().get(AccessMethod.INTERNAL);
     ServiceInterface service = serviceMap.get(ServiceInterface.class);
     String token = serviceMap.get(AuthInterface.class).login(username, password);
     service =
         bimServer
             .getServiceFactory()
             .get(token, AccessMethod.INTERNAL)
             .get(ServiceInterface.class);
     BimDatabase database = bimServer.getDatabase();
     DatabaseSession session = database.createSession();
     SProject firstProjectWithRevisions = null;
     for (SProject project : serviceMap.getServiceInterface().getAllProjects(false, true)) {
       System.out.println(project.getName());
       if (!project.getRevisions().isEmpty() && firstProjectWithRevisions == null) {
         firstProjectWithRevisions = project;
       }
     }
     if (firstProjectWithRevisions != null) {
       long roid = firstProjectWithRevisions.getLastRevisionId();
       DownloadDatabaseAction downloadDatabaseAction =
           new DownloadDatabaseAction(
               bimServer,
               session,
               AccessMethod.INTERNAL,
               roid,
               -1,
               -1,
               ((ServiceImpl) service).getAuthorization(),
               null);
       IfcModelInterface ifcModelInterface = downloadDatabaseAction.execute();
       for (IfcWall ifcWall : ifcModelInterface.getAllWithSubTypes(IfcWall.class)) {
         System.out.println(ifcWall.getName());
       }
     }
   } catch (ServiceException e) {
     e.printStackTrace();
   } catch (BimserverDatabaseException e) {
     e.printStackTrace();
   } catch (PublicInterfaceNotFoundException e) {
     e.printStackTrace();
   }
 }
  private void start() {
    BimServerConfig config = new BimServerConfig();
    Path homeDir = Paths.get("home");
    try {
      if (Files.isDirectory(homeDir)) {
        PathUtils.removeDirectoryWithContent(homeDir);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    config.setClassPath(System.getProperty("java.class.path"));
    config.setHomeDir(homeDir);
    config.setPort(8080);
    config.setStartEmbeddedWebServer(true);
    config.setResourceFetcher(new LocalDevelopmentResourceFetcher(Paths.get("../")));
    final BimServer bimServer = new BimServer(config);
    try {
      LocalDevPluginLoader.loadPlugins(bimServer.getPluginManager(), null);
      bimServer.start();
      if (bimServer.getServerInfo().getServerState() == ServerState.NOT_SETUP) {
        bimServer
            .getService(AdminInterface.class)
            .setup(
                "http://localhost",
                "localhost",
                "*****@*****.**",
                "Administrator",
                "*****@*****.**",
                "admin");
      }
    } catch (PluginException e2) {
      e2.printStackTrace();
    } catch (ServerException e) {
      e.printStackTrace();
    } catch (DatabaseInitException e) {
      e.printStackTrace();
    } catch (BimserverDatabaseException e) {
      e.printStackTrace();
    } catch (DatabaseRestartRequiredException e) {
      e.printStackTrace();
    } catch (UserException e) {
      e.printStackTrace();
    }

    try {
      final ServiceMap serviceMap = bimServer.getServiceFactory().get(AccessMethod.INTERNAL);
      ServiceInterface serviceInterface = serviceMap.get(ServiceInterface.class);
      SettingsInterface settingsInterface = serviceMap.get(SettingsInterface.class);
      final Bimsie1AuthInterface authInterface = serviceMap.get(Bimsie1AuthInterface.class);
      serviceInterface =
          bimServer
              .getServiceFactory()
              .get(authInterface.login("*****@*****.**", "admin"), AccessMethod.INTERNAL)
              .get(ServiceInterface.class);
      settingsInterface.setCacheOutputFiles(true);
      settingsInterface.setGenerateGeometryOnCheckin(false);
      final SProject project =
          serviceMap.getBimsie1ServiceInterface().addProject("test", "ifc2x3tc1");
      SDeserializerPluginConfiguration deserializerByName =
          serviceMap.getBimsie1ServiceInterface().getDeserializerByName("IfcStepDeserializer");
      Path file = Paths.get("../TestData/data/AC11-Institute-Var-2-IFC.ifc");
      serviceInterface.checkin(
          project.getOid(),
          "test",
          deserializerByName.getOid(),
          file.toFile().length(),
          file.getFileName().toString(),
          new DataHandler(new FileDataSource(file.toFile())),
          false,
          true);
      final SProject projectUpdate =
          serviceMap.getBimsie1ServiceInterface().getProjectByPoid(project.getOid());
      ThreadPoolExecutor executor =
          new ThreadPoolExecutor(20, 20, 1, TimeUnit.HOURS, new ArrayBlockingQueue<Runnable>(1000));
      for (int i = 0; i < 20; i++) {
        executor.execute(
            new Runnable() {
              @Override
              public void run() {
                try {
                  ServiceMap serviceMap2 =
                      bimServer
                          .getServiceFactory()
                          .get(
                              authInterface.login("*****@*****.**", "admin"),
                              AccessMethod.INTERNAL);
                  SSerializerPluginConfiguration serializerPluginConfiguration =
                      serviceMap.getBimsie1ServiceInterface().getSerializerByName("Ifc2x3");
                  Long download =
                      serviceMap2
                          .getBimsie1ServiceInterface()
                          .download(
                              projectUpdate.getLastRevisionId(),
                              serializerPluginConfiguration.getOid(),
                              true,
                              true);
                  SDownloadResult downloadData =
                      serviceMap2.getBimsie1ServiceInterface().getDownloadData(download);
                  if (downloadData.getFile().getDataSource()
                      instanceof CacheStoringEmfSerializerDataSource) {
                    CacheStoringEmfSerializerDataSource c =
                        (CacheStoringEmfSerializerDataSource)
                            downloadData.getFile().getDataSource();
                    try {
                      ByteArrayOutputStream baos = new ByteArrayOutputStream();
                      c.writeToOutputStream(baos, null);
                      System.out.println(baos.size());
                    } catch (SerializerException e) {
                      e.printStackTrace();
                    }
                  } else {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    IOUtils.copy(downloadData.getFile().getInputStream(), baos);
                    System.out.println(baos.size());
                  }
                  serviceMap2.getServiceInterface().cleanupLongAction(download);
                } catch (ServerException e) {
                  e.printStackTrace();
                } catch (UserException e) {
                  e.printStackTrace();
                } catch (FileNotFoundException e) {
                  e.printStackTrace();
                } catch (IOException e) {
                  e.printStackTrace();
                } catch (PublicInterfaceNotFoundException e1) {
                  e1.printStackTrace();
                }
              }
            });
      }
      executor.shutdown();
      executor.awaitTermination(1, TimeUnit.HOURS);
      bimServer.stop();
    } catch (ServerException e1) {
      e1.printStackTrace();
    } catch (UserException e1) {
      e1.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 4
0
  @Override
  public void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {
      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");
      boolean useGzip = true;
      OutputStream outputStream = response.getOutputStream();
      boolean zip = request.getParameter("zip") != null && request.getParameter("zip").equals("on");
      if (useGzip && !zip) {
        response.setHeader("Content-Encoding", "gzip");
        outputStream = new GZIPOutputStream(response.getOutputStream());
      }
      String token = (String) request.getSession().getAttribute("token");

      if (token == null) {
        token = request.getParameter("token");
      }
      ServiceMap serviceMap = getBimServer().getServiceFactory().get(token, AccessMethod.INTERNAL);

      String action = request.getParameter("action");
      if (action != null) {
        if (action.equals("extendeddata")) {
          SExtendedData sExtendedData =
              serviceMap
                  .getBimsie1ServiceInterface()
                  .getExtendedData(Long.parseLong(request.getParameter("edid")));
          SFile file = serviceMap.getServiceInterface().getFile(sExtendedData.getFileId());
          if (file.getMime() != null) {
            response.setContentType(file.getMime());
          }
          if (file.getFilename() != null) {
            response.setHeader(
                "Content-Disposition", "inline; filename=\"" + file.getFilename() + "\"");
          }
          outputStream.write(file.getData());
          if (outputStream instanceof GZIPOutputStream) {
            ((GZIPOutputStream) outputStream).finish();
          }
          outputStream.flush();
          return;
        } else if (action.equals("getfile")) {
          String type = request.getParameter("type");
          if (type.equals("proto")) {
            try {
              String protocolBuffersFile =
                  serviceMap
                      .getAdminInterface()
                      .getProtocolBuffersFile(request.getParameter("name"));
              outputStream.write(protocolBuffersFile.getBytes(Charsets.UTF_8));
              outputStream.flush();
            } catch (ServiceException e) {
              LOGGER.error("", e);
            }
          } else if (type.equals("serverlog")) {
            try {
              OutputStreamWriter writer = new OutputStreamWriter(outputStream);
              writer.write(serviceMap.getAdminInterface().getServerLog());
              writer.flush();
            } catch (ServerException e) {
              LOGGER.error("", e);
            } catch (UserException e) {
              LOGGER.error("", e);
            }
          }
        }
      } else {
        SSerializerPluginConfiguration serializer = null;
        if (request.getParameter("serializerOid") != null) {
          long serializerOid = Long.parseLong(request.getParameter("serializerOid"));
          serializer = serviceMap.getBimsie1ServiceInterface().getSerializerById(serializerOid);
        } else {
          serializer =
              serviceMap
                  .getBimsie1ServiceInterface()
                  .getSerializerByName(request.getParameter("serializerName"));
        }
        long downloadId = -1;
        if (request.getParameter("longActionId") != null) {
          downloadId = Integer.parseInt(request.getParameter("longActionId"));
        } else if (request.getParameter("multiple") != null) {
          Set<Long> roids = new HashSet<Long>();
          for (Object key : request.getParameterMap().keySet()) {
            String keyString = (String) key;
            if (keyString.startsWith("download_")) {
              if (!request.getParameter(keyString).equals("[off]")) {
                roids.add(Long.parseLong(request.getParameter(keyString)));
              }
            }
          }
          downloadId =
              serviceMap
                  .getBimsie1ServiceInterface()
                  .downloadRevisions(roids, serializer.getOid(), true);
        } else if (request.getParameter("compare") != null) {
          SCompareType sCompareType = SCompareType.valueOf(request.getParameter("type"));
          Long roid1 = Long.parseLong(request.getParameter("roid1"));
          Long roid2 = Long.parseLong(request.getParameter("roid2"));
          downloadId =
              serviceMap
                  .getServiceInterface()
                  .downloadCompareResults(
                      serializer.getOid(),
                      roid1,
                      roid2,
                      Long.valueOf(request.getParameter("mcid")),
                      sCompareType,
                      true);
        } else {
          long roid = -1;
          if (request.getParameter("roid") == null) {
            if (request.getParameter("poid") != null) {
              long poid = Long.parseLong(request.getParameter("poid"));
              SProject projectByPoid =
                  serviceMap.getBimsie1ServiceInterface().getProjectByPoid(poid);
              if (projectByPoid == null) {
                throw new UserException("Project with oid " + poid + " not found");
              }
              roid = projectByPoid.getLastRevisionId();
              if (roid == -1) {
                throw new UserException("No revisions");
              }
            } else {
              throw new UserException("A poid or roid is required for downloading");
            }
          } else {
            roid = Long.parseLong(request.getParameter("roid"));
          }
          if (request.getParameter("checkout") != null) {
            downloadId =
                serviceMap.getBimsie1ServiceInterface().checkout(roid, serializer.getOid(), true);
          } else {
            if (request.getParameter("classses") != null) {
              Set<String> classes = new HashSet<String>();
              for (String className : request.getParameter("classses").split(";")) {
                classes.add(className);
              }
              Set<Long> roids = new HashSet<Long>();
              roids.add(roid);
              downloadId =
                  serviceMap
                      .getBimsie1ServiceInterface()
                      .downloadByTypes(
                          roids, classes, serializer.getOid(), false, true, true, true);
            } else if (request.getParameter("oids") != null) {
              Set<Long> oids = new HashSet<Long>();
              for (String oidString : request.getParameter("oids").split(";")) {
                oids.add(Long.parseLong(oidString));
              }
              Set<Long> roids = new HashSet<Long>();
              roids.add(roid);
              downloadId =
                  serviceMap
                      .getBimsie1ServiceInterface()
                      .downloadByOids(roids, oids, serializer.getOid(), true, true);
            } else if (request.getParameter("guids") != null) {
              Set<String> guids = new HashSet<String>();
              for (String guid : request.getParameter("guids").split(";")) {
                guids.add(guid);
              }
              Set<Long> roids = new HashSet<Long>();
              roids.add(roid);
              downloadId =
                  serviceMap
                      .getBimsie1ServiceInterface()
                      .downloadByGuids(roids, guids, serializer.getOid(), false, true);
            } else {
              downloadId =
                  serviceMap
                      .getBimsie1ServiceInterface()
                      .download(roid, serializer.getOid(), true, true);
            }
          }
        }
        if (downloadId == -1) {
          response.getWriter().println("No valid download");
          return;
        }
        SDownloadResult checkoutResult =
            serviceMap.getBimsie1ServiceInterface().getDownloadData(downloadId);
        if (checkoutResult == null) {
          LOGGER.error("Invalid downloadId: " + downloadId);
        } else {
          DataSource dataSource = checkoutResult.getFile().getDataSource();
          PluginConfiguration pluginConfiguration =
              new PluginConfiguration(
                  serviceMap.getPluginInterface().getPluginSettings(serializer.getOid()));
          if (zip) {
            if (pluginConfiguration.getString("ZipExtension") != null) {
              response.setHeader(
                  "Content-Disposition",
                  "inline; filename=\""
                      + dataSource.getName()
                      + "."
                      + pluginConfiguration.getString(SerializerPlugin.ZIP_EXTENSION)
                      + "\"");
            } else {
              response.setHeader(
                  "Content-Disposition",
                  "inline; filename=\"" + dataSource.getName() + ".zip" + "\"");
            }
            response.setContentType("application/zip");
            String nameInZip =
                checkoutResult.getProjectName()
                    + "."
                    + checkoutResult.getRevisionNr()
                    + "."
                    + pluginConfiguration.getString(SerializerPlugin.EXTENSION);
            ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
            zipOutputStream.putNextEntry(new ZipEntry(nameInZip));
            if (dataSource instanceof FileInputStreamDataSource) {
              InputStream inputStream = ((FileInputStreamDataSource) dataSource).getInputStream();
              IOUtils.copy(inputStream, zipOutputStream);
              inputStream.close();
            } else {
              ((EmfSerializerDataSource) dataSource).writeToOutputStream(zipOutputStream);
            }
            try {
              zipOutputStream.finish();
            } catch (IOException e) {
              // Sometimes it's already closed, that's no problem
            }
          } else {
            if (request.getParameter("mime") == null) {
              response.setContentType(pluginConfiguration.getString(SerializerPlugin.CONTENT_TYPE));
              response.setHeader(
                  "Content-Disposition",
                  "inline; filename=\""
                      + dataSource.getName()
                      + "."
                      + pluginConfiguration.getString(SerializerPlugin.EXTENSION)
                      + "\"");
            } else {
              response.setContentType(request.getParameter("mime"));
            }
            try {
              if (dataSource instanceof FileInputStreamDataSource) {
                InputStream inputStream = ((FileInputStreamDataSource) dataSource).getInputStream();
                IOUtils.copy(inputStream, outputStream);
                inputStream.close();
              } else {
                ((EmfSerializerDataSource) dataSource).writeToOutputStream(outputStream);
              }
            } catch (SerializerException e) {
              LOGGER.error("", e);
            }
          }
        }
      }
      if (outputStream instanceof GZIPOutputStream) {
        ((GZIPOutputStream) outputStream).finish();
      }
      outputStream.flush();
    } catch (NumberFormatException e) {
      LOGGER.error("", e);
      response.getWriter().println("Some number was incorrectly formatted");
    } catch (ServiceException e) {
      LOGGER.error("", e);
      response.getWriter().println(e.getUserMessage());
    } catch (EOFException e) {
    } catch (Exception e) {
      LOGGER.error("", e);
    }
  }