Example #1
0
 /**
  * Cleans up the given store.
  *
  * @param crdf could be a triple or a quad store.
  * @throws CumulusStoreException in case the cleanup fails.
  */
 public static void clean(final Store crdf) throws CumulusStoreException {
   crdf.clear();
   assertEquals(
       "Store seems not empty after issuing a delete * command.",
       0,
       numOfRes(
           crdf.query(
               crdf instanceof TripleStore
                   ? SELECT_ALL_TRIPLES_PATTERN
                   : SELECT_ALL_QUADS_PATTERN)));
 }
Example #2
0
  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    long start = System.currentTimeMillis();
    ServletContext ctx = getServletContext();
    if (req.getCharacterEncoding() == null) req.setCharacterEncoding("UTF-8");
    resp.setCharacterEncoding("UTF-8");

    String accept = req.getHeader("Accept");
    SerializationFormat formatter = Listener.getSerializationFormat(accept);
    if (formatter == null) {
      sendError(
          ctx,
          req,
          resp,
          HttpServletResponse.SC_NOT_ACCEPTABLE,
          "no known mime type in Accept header");
      return;
    }

    PrintWriter out_r = resp.getWriter();
    String resp_msg = "";
    // check that we have a file upload request
    boolean isMultipart = ServletFileUpload.isMultipartContent(req);
    if (!isMultipart) {
      sendError(ctx, req, resp, HttpServletResponse.SC_NOT_ACCEPTABLE, "no file upload");
      return;
    }
    // Create a factory for disk-based file items
    FileItemFactory factory = new DiskFileItemFactory();
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    // Parse the request
    try {
      List<FileItem> items = upload.parseRequest(req);
      // bulk load options
      int threads = -1;
      String format = "nt";
      int batchSizeMB = 1;
      // get store
      AbstractCassandraRdfHector crdf =
          (AbstractCassandraRdfHector) ctx.getAttribute(Listener.STORE);
      crdf.setBatchSize(batchSizeMB);

      // Process the uploaded items
      Iterator iter = items.iterator();
      String a = "";
      String graph = "";
      boolean a_exists = false;
      String file = "";

      while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();
        if (item.isFormField()) {
          String name = item.getFieldName();
          String value = item.getString();
          if (name.equals("g")) {
            a_exists = true;
            a = new String(value);
            // escape if the accept header is not text/plain
            graph = new String(a);
            if (formatter.getContentType().equals("text/html")) graph = escapeHtml(a);
          }
        } else {
          String fieldName = item.getFieldName();
          String fileName = item.getName();
          String contentType = item.getContentType();
          boolean isInMemory = item.isInMemory();
          long sizeInBytes = item.getSize();
          // Process a file upload
          InputStream uploadedStream = item.getInputStream();
          // write the inputStream to a FileOutputStream
          file = "/tmp/upload_bulkload_" + UUID.randomUUID();
          OutputStream out = new FileOutputStream(new File(file));
          int read = 0;
          byte[] bytes = new byte[1024];
          while ((read = uploadedStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
          }
          uploadedStream.close();
          out.flush();
          out.close();
          resp_msg += "Bulkload " + fileName + ", size " + sizeInBytes;
        }
      }
      if (!a_exists || a == null || a.isEmpty()) {
        sendError(
            ctx,
            req,
            resp,
            HttpServletResponse.SC_BAD_REQUEST,
            "Please pass also the graph name as 'g' parameter");
      } else {
        if (!a.startsWith("<") || !a.endsWith(">")) {
          sendError(
              ctx,
              req,
              resp,
              HttpServletResponse.SC_BAD_REQUEST,
              "Please pass a resource as the graph name.");
          return;
        }

        // load here
        // note: if threads==-1, it will be then set to the number of hosts
        if (crdf.bulkLoad(new File(file), format, threads, Store.encodeKeyspace(a)) == 1)
          sendError(
              ctx,
              req,
              resp,
              HttpServletResponse.SC_CONFLICT,
              "Graph " + graph + " does not exist yet. Please create it before bulk loading.");
        else {
          resp_msg = "Graph " + graph + " time " + (System.currentTimeMillis() - start) + "ms";
          sendResponse(ctx, req, resp, HttpServletResponse.SC_BAD_REQUEST, resp_msg);
          _log.info(resp_msg);
        }
      }
      // delete the tmp file
      new File(file).delete();
      out_r.println(resp_msg);
      out_r.close();
    } catch (FileUploadException ex) {
      ex.printStackTrace();
      return;
    } catch (StoreException ex) {
      ex.printStackTrace();
      return;
    }
    return;
  }