protected void replaceResourceWithStream(
      final FedoraResource resource,
      final InputStream requestBodyStream,
      final MediaType contentType,
      final RdfStream resourceTriples)
      throws MalformedRdfException {
    final Lang format = contentTypeToLang(contentType.toString());

    final Model inputModel = createDefaultModel();
    try {
      inputModel.read(
          requestBodyStream, getUri(resource).toString(), format.getName().toUpperCase());

    } catch (final RiotException e) {
      throw new BadRequestException("RDF was not parsable: " + e.getMessage(), e);

    } catch (final RuntimeIOException e) {
      if (e.getCause() instanceof JsonParseException) {
        throw new MalformedRdfException(e.getCause());
      }
      throw new RepositoryRuntimeException(e);
    }

    resource.replaceProperties(translator(), inputModel, resourceTriples);
  }
Ejemplo n.º 2
0
  @Override
  protected void doGet(HttpAction action) {
    // Assume success - do the set up before grabbing the lock.
    // Sets content type.
    MediaType mediaType = ActionLib.contentNegotationRDF(action);

    ServletOutputStream output;
    try {
      output = action.response.getOutputStream();
    } catch (IOException ex) {
      ServletOps.errorOccurred(ex);
      output = null;
    }

    TypedOutputStream out = new TypedOutputStream(output, mediaType);
    Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());

    if (action.verbose)
      action.log.info(
          format(
              "[%d]   Get: Content-Type=%s, Charset=%s => %s",
              action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));

    action.beginRead();
    setCommonHeaders(action.response);
    try {
      Target target = determineTarget(action);
      if (action.log.isDebugEnabled()) action.log.debug("GET->" + target);
      boolean exists = target.exists();
      if (!exists) ServletOps.errorNotFound("No such graph: <" + target.name + ">");
      // If we want to set the Content-Length, we need to buffer.
      // response.setContentLength(??) ;
      String ct = lang.getContentType().toHeaderString();
      action.response.setContentType(ct);
      Graph g = target.graph();
      // Special case RDF/XML to be the plain (faster, less readable) form
      RDFFormat fmt =
          (lang == Lang.RDFXML)
              ? RDFFormat.RDFXML_PLAIN
              : RDFWriterRegistry.defaultSerialization(lang);
      try {
        RDFDataMgr.write(out, g, fmt);
      } catch (JenaException ex) {
        // Some RDF/XML data is unwritable. All we can do is pretend it's a bad
        // request (inappropriate content type).
        // Good news - this happens before any output for RDF/XML-ABBREV.
        if (fmt.getLang().equals(Lang.RDFXML))
          ServletOps.errorBadRequest("Failed to write output in RDF/XML: " + ex.getMessage());
        else ServletOps.errorOccurred("Failed to write output: " + ex.getMessage(), ex);
      }
      ServletOps.success(action);
    } finally {
      action.endRead();
    }
  }
  /**
   * This method returns an HTTP response with content body appropriate to the following arguments.
   *
   * @param rangeValue starting and ending byte offsets, see {@link Range}
   * @param limit is the number of child resources returned in the response, -1 for all
   * @param rdfStream to which response RDF will be concatenated
   * @return HTTP response
   * @throws IOException
   */
  protected Response getContent(final String rangeValue, final int limit, final RdfStream rdfStream)
      throws IOException {
    if (resource() instanceof FedoraBinary) {

      final String contentTypeString = ((FedoraBinary) resource()).getMimeType();

      final Lang lang = contentTypeToLang(contentTypeString);

      if (!contentTypeString.equals("text/plain") && lang != null) {

        final String format = lang.getName().toUpperCase();

        final InputStream content = ((FedoraBinary) resource()).getContent();

        final Model inputModel =
            createDefaultModel().read(content, (resource()).toString(), format);

        rdfStream.concat(Iterators.transform(inputModel.listStatements(), Statement::asTriple));
      } else {

        final MediaType mediaType = MediaType.valueOf(contentTypeString);
        if (MESSAGE_EXTERNAL_BODY.isCompatible(mediaType)
            && mediaType.getParameters().containsKey("access-type")
            && mediaType.getParameters().get("access-type").equals("URL")
            && mediaType.getParameters().containsKey("URL")) {
          try {
            return temporaryRedirect(new URI(mediaType.getParameters().get("URL"))).build();
          } catch (final URISyntaxException e) {
            throw new RepositoryRuntimeException(e);
          }
        }
        return getBinaryContent(rangeValue);
      }

    } else {
      rdfStream.concat(getResourceTriples(limit));
      if (prefer != null) {
        prefer.getReturn().addResponseHeaders(servletResponse);
      }
    }
    servletResponse.addHeader("Vary", "Accept, Range, Accept-Encoding, Accept-Language");

    return ok(rdfStream).build();
  }