예제 #1
0
  public static void main(String[] args) {
    BimServerClientInterface client = LocalDevSetup.setupJson("http://localhost:8080");
    try {
      SProject project =
          client.getBimsie1ServiceInterface().addProject("testProject2", "ifc2x3tc1");

      Long tid = client.getBimsie1LowLevelInterface().startTransaction(project.getOid());
      client.getBimsie1LowLevelInterface().createObject(tid, "IfcWall", false);
      Long roid = client.getBimsie1LowLevelInterface().commitTransaction(tid, "test commit");

      SSerializerPluginConfiguration serializer =
          client.getBimsie1ServiceInterface().getSerializerByContentType("application/ifc");
      client.download(roid, serializer.getOid(), new File("test2.ifc"));
    } catch (ServerException | UserException | PublicInterfaceNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
예제 #2
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);
    }
  }
예제 #3
0
  @Test
  public void test() {
    try {
      // Create a new BimServerClient with authentication
      BimServerClientInterface bimServerClient =
          getFactory()
              .create(new UsernamePasswordAuthenticationInfo("*****@*****.**", "admin"));

      long s = System.nanoTime();
      // Create a new project
      SProject mainProject =
          bimServerClient.getBimsie1ServiceInterface().addProject("main" + Math.random());
      SProject sub1 =
          bimServerClient
              .getBimsie1ServiceInterface()
              .addProjectAsSubProject("Sub1" + Math.random(), mainProject.getOid());
      SProject sub2 =
          bimServerClient
              .getBimsie1ServiceInterface()
              .addProjectAsSubProject("Sub2" + Math.random(), mainProject.getOid());
      SProject sub3 =
          bimServerClient
              .getBimsie1ServiceInterface()
              .addProjectAsSubProject("Sub3" + Math.random(), mainProject.getOid());

      // This is the file we will be checking in
      File ifcFile1 = new File("../TestData/data/AC11-Institute-Var-2-IFC.ifc");
      File ifcFile2 = new File("../TestData/data/AC90R1-niedriha-V2-2x3.ifc");
      File ifcFile3 = new File("../TestData/data/AC11-FZK-Haus-IFC.ifc");

      // Find a deserializer to use
      SDeserializerPluginConfiguration deserializer =
          bimServerClient.getBimsie1ServiceInterface().getSuggestedDeserializerForExtension("ifc");

      // Checkin
      bimServerClient.checkin(sub1.getOid(), "test", deserializer.getOid(), false, true, ifcFile1);
      bimServerClient.checkin(sub2.getOid(), "test", deserializer.getOid(), false, true, ifcFile2);
      bimServerClient.checkin(sub3.getOid(), "test", deserializer.getOid(), false, true, ifcFile3);

      // Find a serializer
      SSerializerPluginConfiguration serializer =
          bimServerClient
              .getBimsie1ServiceInterface()
              .getSerializerByContentType("application/ifc");

      // Get the project details
      mainProject =
          bimServerClient.getBimsie1ServiceInterface().getProjectByPoid(mainProject.getOid());

      // Download the latest revision (the one we just checked in)
      //			Long downloadId =
      // bimServerClient.getBimsie1ServiceInterface().downloadByTypes(Collections.singleton(mainProject.getLastRevisionId()),
      //					Collections.singleton("IfcWall"), serializer.getOid(), true, false, true, true);
      Long downloadId =
          bimServerClient
              .getBimsie1ServiceInterface()
              .download(mainProject.getLastRevisionId(), serializer.getOid(), true, true);
      IOUtils.copy(
          bimServerClient.getDownloadData(downloadId, serializer.getOid()),
          new FileOutputStream(new File("out.ifc")));
      long e = System.nanoTime();
      System.out.println(((e - s) / 1000000) + " ms");
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }
예제 #4
0
  @Test
  public void test() {
    try {
      // Create a new BimServerClient with authentication
      BimServerClientInterface bimServerClient =
          getFactory()
              .create(new UsernamePasswordAuthenticationInfo("*****@*****.**", "admin"));

      // Create a new project
      SProject newProject =
          bimServerClient
              .getBimsie1ServiceInterface()
              .addProject("test" + Math.random(), "ifc2x3tc1");

      // Get the appropriate deserializer
      SDeserializerPluginConfiguration deserializer =
          bimServerClient
              .getBimsie1ServiceInterface()
              .getSuggestedDeserializerForExtension("ifc", newProject.getOid());

      // Checkin the file
      bimServerClient.checkin(
          newProject.getOid(),
          "test",
          deserializer.getOid(),
          false,
          true,
          new File("../TestData/data/AC11-Institute-Var-2-IFC.ifc"));

      // Refresh project info
      newProject =
          bimServerClient.getBimsie1ServiceInterface().getProjectByPoid(newProject.getOid());

      IfcModelInterface model =
          bimServerClient.getModel(newProject, newProject.getLastRevisionId(), true, false);
      for (IfcFurnishingElement ifcFurnishingElement :
          model.getAllWithSubTypes(IfcFurnishingElement.class)) {
        IfcObjectPlacement objectPlacement = ifcFurnishingElement.getObjectPlacement();
        if (objectPlacement != null && objectPlacement instanceof IfcLocalPlacement) {
          IfcLocalPlacement localPlacement = (IfcLocalPlacement) objectPlacement;
          IfcAxis2Placement relativePlacement = localPlacement.getRelativePlacement();
          if (relativePlacement != null) {
            if (relativePlacement instanceof IfcAxis2Placement3D) {
              IfcAxis2Placement3D axis2Placement3D = (IfcAxis2Placement3D) relativePlacement;
              IfcCartesianPoint location = axis2Placement3D.getLocation();
              double newValue = location.getCoordinates().get(2) + 50;
              System.out.println(
                  "Changing z value of "
                      + ifcFurnishingElement.getName()
                      + " from "
                      + location.getCoordinates().get(2)
                      + " to "
                      + newValue);
              location.getCoordinates().set(2, newValue);
            }
          }
        }
      }
      long newRoid = model.commit("Moved all furniture 50 meters up");
      SSerializerPluginConfiguration ifcSerializer =
          bimServerClient
              .getBimsie1ServiceInterface()
              .getSerializerByContentType("application/ifc");
      bimServerClient.download(newRoid, ifcSerializer.getOid(), new File("movedf.ifc"));
    } catch (Throwable e) {
      e.printStackTrace();
      if (e instanceof AssertionError) {
        throw (AssertionError) e;
      }
      fail(e.getMessage());
    }
  }