private static void doBundle(ResourceImpl resource, Headers headers) {
   CapabilityImpl capability = new CapabilityImpl(Capability.BUNDLE);
   capability.addProperty(Resource.SYMBOLIC_NAME, getSymbolicName(headers));
   if (headers.getHeader(Constants.BUNDLE_NAME) != null) {
     capability.addProperty(Resource.PRESENTATION_NAME, headers.getHeader(Constants.BUNDLE_NAME));
   }
   capability.addProperty(Resource.VERSION, Property.VERSION, getVersion(headers));
   capability.addProperty(Resource.MANIFEST_VERSION, getManifestVersion(headers));
   resource.addCapability(capability);
 }
 private static String getSymbolicName(Headers headers) {
   String bsn = headers.getHeader(Constants.BUNDLE_SYMBOLICNAME);
   if (bsn == null) {
     bsn = headers.getHeader(Constants.BUNDLE_NAME);
     if (bsn == null) {
       bsn = "Untitled-" + headers.hashCode();
     }
   }
   Clause[] clauses = Parser.parseHeader(bsn);
   return clauses[0].getName();
 }
 private static String getManifestVersion(Headers headers) {
   String v = headers.getHeader(Constants.BUNDLE_MANIFESTVERSION);
   if (v == null) {
     v = "1";
   }
   return v;
 }
 private static void doExports(ResourceImpl resource, Headers headers) {
   Clause[] clauses = Parser.parseHeader(headers.getHeader(Constants.EXPORT_PACKAGE));
   for (int i = 0; clauses != null && i < clauses.length; i++) {
     CapabilityImpl capability = createCapability(Capability.PACKAGE, clauses[i]);
     resource.addCapability(capability);
   }
 }
  private static void doFragment(ResourceImpl resource, Headers headers) {
    // Check if we are a fragment
    Clause[] clauses = Parser.parseHeader(headers.getHeader(Constants.FRAGMENT_HOST));
    if (clauses != null && clauses.length == 1) {
      // We are a fragment, create a requirement
      // to our host.
      RequirementImpl r = new RequirementImpl(Capability.BUNDLE);
      StringBuffer sb = new StringBuffer();
      sb.append("(&(symbolicname=");
      sb.append(clauses[0].getName());
      sb.append(")");
      appendVersion(
          sb,
          VersionRange.parseVersionRange(
              clauses[0].getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE)));
      sb.append(")");
      r.setFilter(sb.toString());
      r.addText("Required Host " + clauses[0].getName());
      r.setExtend(true);
      r.setOptional(false);
      r.setMultiple(false);
      resource.addRequire(r);

      // And insert a capability that we are available
      // as a fragment. ### Do we need that with extend?
      CapabilityImpl capability = new CapabilityImpl(Capability.FRAGMENT);
      capability.addProperty("host", clauses[0].getName());
      capability.addProperty("version", Property.VERSION, getVersion(clauses[0]));
      resource.addCapability(capability);
    }
  }
 public ResourceImpl createResource(Headers headers) {
   String bsn = headers.getHeader(Constants.BUNDLE_SYMBOLICNAME);
   if (bsn == null) {
     return null;
   }
   ResourceImpl resource = new ResourceImpl();
   populate(headers, resource);
   return resource;
 }
Beispiel #7
0
 public void sendResponseHeaders(int rCode, long contentLen) throws IOException {
   if (sentHeaders) {
     throw new IOException("headers already sent");
   }
   this.rcode = rCode;
   String statusLine = "HTTP/1.1 " + rCode + Code.msg(rCode) + "\r\n";
   OutputStream tmpout = new BufferedOutputStream(ros);
   PlaceholderOutputStream o = getPlaceholderResponseBody();
   tmpout.write(bytes(statusLine, 0), 0, statusLine.length());
   boolean noContentToSend = false; // assume there is content
   rspHdrs.set("Date", df.format(new Date()));
   if (contentLen == 0) {
     if (http10) {
       o.setWrappedStream(new UndefLengthOutputStream(this, ros));
       close = true;
     } else {
       rspHdrs.set("Transfer-encoding", "chunked");
       o.setWrappedStream(new ChunkedOutputStream(this, ros));
     }
   } else {
     if (contentLen == -1) {
       noContentToSend = true;
       contentLen = 0;
     }
     /* content len might already be set, eg to implement HEAD resp */
     if (rspHdrs.getFirst("Content-length") == null) {
       rspHdrs.set("Content-length", Long.toString(contentLen));
     }
     o.setWrappedStream(new FixedLengthOutputStream(this, ros, contentLen));
   }
   write(rspHdrs, tmpout);
   this.rspContentLen = contentLen;
   tmpout.flush();
   tmpout = null;
   sentHeaders = true;
   if (noContentToSend) {
     WriteFinishedEvent e = new WriteFinishedEvent(this);
     server.addEvent(e);
     closed = true;
   }
   server.logReply(rCode, req.requestLine(), null);
 }
  private static void doImportExportServices(ResourceImpl resource, Headers headers) {
    Clause[] imports = Parser.parseHeader(headers.getHeader(Constants.IMPORT_SERVICE));
    for (int i = 0; imports != null && i < imports.length; i++) {
      RequirementImpl ri = new RequirementImpl(Capability.SERVICE);
      ri.setFilter(createServiceFilter(imports[i]));
      ri.addText("Import Service " + imports[i].getName());

      String avail = imports[i].getDirective("availability");
      String mult = imports[i].getDirective("multiple");
      ri.setOptional("optional".equalsIgnoreCase(avail));
      ri.setMultiple(!"false".equalsIgnoreCase(mult));
      resource.addRequire(ri);
    }

    Clause[] exports = Parser.parseHeader(headers.getHeader(Constants.EXPORT_SERVICE));
    for (int i = 0; exports != null && i < exports.length; i++) {
      CapabilityImpl cap = createServiceCapability(exports[i]);
      resource.addCapability(cap);
    }
  }
 @Override
 public void handle(HttpExchange t) throws IOException {
   if (RemoteUtil.deny(t)) {
     throw new IOException("Access denied");
   }
   String id = RemoteUtil.getId("thumb/", t);
   LOGGER.trace("web thumb req " + id);
   if (id.contains("logo")) {
     RemoteUtil.sendLogo(t);
     return;
   }
   RootFolder root = parent.getRoot(RemoteUtil.userName(t), t);
   if (root == null) {
     LOGGER.debug("weird root in thumb req");
     throw new IOException("Unknown root");
   }
   final DLNAResource r = root.getDLNAResource(id, root.getDefaultRenderer());
   if (r == null) {
     // another error
     LOGGER.debug("media unknown");
     throw new IOException("Bad id");
   }
   InputStream in;
   if (!configuration.isShowCodeThumbs() && !r.isCodeValid(r)) {
     // we shouldn't show the thumbs for coded objects
     // unless the code is entered
     in = r.getGenericThumbnailInputStream(null);
   } else {
     r.checkThumbnail();
     in = r.getThumbnailInputStream();
   }
   Headers hdr = t.getResponseHeaders();
   hdr.add("Content-Type", r.getThumbnailContentType());
   hdr.add("Accept-Ranges", "bytes");
   hdr.add("Connection", "keep-alive");
   t.sendResponseHeaders(200, in.available());
   OutputStream os = t.getResponseBody();
   LOGGER.trace("input is {} output is {}", in, os);
   RemoteUtil.dump(in, os);
 }
Beispiel #10
0
  private static void doImports(ResourceImpl resource, Headers headers) {
    Clause[] clauses = Parser.parseHeader(headers.getHeader(Constants.IMPORT_PACKAGE));
    for (int i = 0; clauses != null && i < clauses.length; i++) {
      RequirementImpl requirement = new RequirementImpl(Capability.PACKAGE);

      createImportFilter(requirement, Capability.PACKAGE, clauses[i]);
      requirement.addText("Import package " + clauses[i]);
      requirement.setOptional(
          Constants.RESOLUTION_OPTIONAL.equalsIgnoreCase(
              clauses[i].getDirective(Constants.RESOLUTION_DIRECTIVE)));
      resource.addRequire(requirement);
    }
  }
Beispiel #11
0
 private static void doExecutionEnvironment(ResourceImpl resource, Headers headers) {
   Clause[] clauses =
       Parser.parseHeader(headers.getHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT));
   if (clauses != null && clauses.length > 0) {
     StringBuffer sb = new StringBuffer();
     sb.append("(|");
     for (int i = 0; i < clauses.length; i++) {
       sb.append("(");
       sb.append(Capability.EXECUTIONENVIRONMENT);
       sb.append("=");
       sb.append(clauses[i].getName());
       sb.append(")");
     }
     sb.append(")");
     RequirementImpl req = new RequirementImpl(Capability.EXECUTIONENVIRONMENT);
     req.setFilter(sb.toString());
     req.addText("Execution Environment " + sb.toString());
     resource.addRequire(req);
   }
 }
Beispiel #12
0
 void write(Headers map, OutputStream os) throws IOException {
   Set<Map.Entry<String, List<String>>> entries = map.entrySet();
   for (Map.Entry<String, List<String>> entry : entries) {
     String key = entry.getKey();
     byte[] buf;
     List<String> values = entry.getValue();
     for (String val : values) {
       int i = key.length();
       buf = bytes(key, 2);
       buf[i++] = ':';
       buf[i++] = ' ';
       os.write(buf, 0, i);
       buf = bytes(val, 2);
       i = val.length();
       buf[i++] = '\r';
       buf[i++] = '\n';
       os.write(buf, 0, i);
     }
   }
   os.write('\r');
   os.write('\n');
 }
Beispiel #13
0
  private static void doRequires(ResourceImpl resource, Headers headers) {
    Clause[] clauses = Parser.parseHeader(headers.getHeader(Constants.REQUIRE_BUNDLE));
    for (int i = 0; clauses != null && i < clauses.length; i++) {
      RequirementImpl r = new RequirementImpl(Capability.BUNDLE);

      VersionRange v =
          VersionRange.parseVersionRange(
              clauses[i].getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE));

      StringBuffer sb = new StringBuffer();
      sb.append("(&(symbolicname=");
      sb.append(clauses[i].getName());
      sb.append(")");
      appendVersion(sb, v);
      sb.append(")");
      r.setFilter(sb.toString());

      r.addText("Require Bundle " + clauses[i].getName() + "; " + v);
      r.setOptional(
          Constants.RESOLUTION_OPTIONAL.equalsIgnoreCase(
              clauses[i].getDirective(Constants.RESOLUTION_DIRECTIVE)));
      resource.addRequire(r);
    }
  }
Beispiel #14
0
 private static String getVersion(Headers headers) {
   String v = headers.getHeader(Constants.BUNDLE_VERSION);
   return VersionCleaner.clean(v);
 }
Beispiel #15
0
 private static void doCategories(ResourceImpl resource, Headers headers) {
   Clause[] clauses = Parser.parseHeader(headers.getHeader(Constants.BUNDLE_CATEGORY));
   for (int i = 0; clauses != null && i < clauses.length; i++) {
     resource.addCategory(clauses[i].getName());
   }
 }
Beispiel #16
0
  static void populate(Headers headers, ResourceImpl resource) {
    String bsn = getSymbolicName(headers);
    String v = getVersion(headers);

    resource.put(Resource.ID, bsn + "/" + v);
    resource.put(Resource.SYMBOLIC_NAME, bsn);
    resource.put(Resource.VERSION, v);
    if (headers.getHeader(Constants.BUNDLE_NAME) != null) {
      resource.put(Resource.PRESENTATION_NAME, headers.getHeader(Constants.BUNDLE_NAME));
    }
    if (headers.getHeader(Constants.BUNDLE_DESCRIPTION) != null) {
      resource.put(Resource.DESCRIPTION, headers.getHeader(Constants.BUNDLE_DESCRIPTION));
    }
    if (headers.getHeader(BUNDLE_LICENSE) != null) {
      resource.put(Resource.LICENSE_URI, headers.getHeader(BUNDLE_LICENSE));
    }
    if (headers.getHeader(Constants.BUNDLE_COPYRIGHT) != null) {
      resource.put(Resource.COPYRIGHT, headers.getHeader(Constants.BUNDLE_COPYRIGHT));
    }
    if (headers.getHeader(Constants.BUNDLE_DOCURL) != null) {
      resource.put(Resource.DOCUMENTATION_URI, headers.getHeader(Constants.BUNDLE_DOCURL));
    }
    if (headers.getHeader(BUNDLE_SOURCE) != null) {
      resource.put(Resource.SOURCE_URI, headers.getHeader(BUNDLE_SOURCE));
    }

    doCategories(resource, headers);
    doBundle(resource, headers);
    doImportExportServices(resource, headers);
    doFragment(resource, headers);
    doRequires(resource, headers);
    doExports(resource, headers);
    doImports(resource, headers);
    doExecutionEnvironment(resource, headers);
  }