Ejemplo n.º 1
0
 /**
  * Similar to {@link #readMap(com.linkedin.r2.message.rest.RestMessage)}, but will throw an {@link
  * IOException} instead of a {@link RestLiInternalException}
  *
  * @throws IOException if the message entity cannot be parsed.
  */
 private static DataMap readMapWithExceptions(final RestMessage message) throws IOException {
   try {
     return DataMapConverter.bytesToDataMap(
         message.getHeader(RestConstants.HEADER_CONTENT_TYPE), message.getEntity());
   } catch (MimeTypeParseException e) {
     throw new RoutingException(e.getMessage(), HttpStatus.S_400_BAD_REQUEST.getCode(), e);
   }
 }
  /**
   * Load MIMEType and corresponding extension.
   *
   * @param aLine the a line
   */
  protected void processLine(String aLine) {
    aLine = aLine.toLowerCase();
    int p = aLine.indexOf("=");

    String ext = aLine.substring(0, p);
    String mimetype = aLine.substring(p + 1);

    try {
      MimeType mimeType = new MimeType(mimetype);
      String mode = mimeType.getParameter(ExtendedMimeTypeResolver.X_TYPE_MODE);
      if (mode != null && mode.length() > 0) {
        String baseType = mimeType.getBaseType();
        Set<String> modeList = modes.get(baseType);
        if (modeList == null) {
          modeList = new LinkedHashSet<String>();
        }
        modeList.add(mode);
        modes.put(baseType, modeList);
      }
    } catch (MimeTypeParseException e) {
      LOG.warn("Error parsing mimetype " + mimetype + ": " + e.getMessage());
    }

    // add mimetype
    List<String> values = mimeTypes.get(ext);
    if (values == null) {
      values = new ArrayList<String>();
      mimeTypes.put(ext, values);
    }
    values.add(mimetype);

    // add extension
    values = extentions.get(mimetype);
    if (values == null) {
      values = new ArrayList<String>();
      extentions.put(mimetype, values);
    }
    values.add(ext);
  }
 /**
  * Return optional representation (UI) mode parameter (x-type-mode) for given MIME type or/and a
  * file name. If type is <code>null</code> then the type will be defined from given file name
  * first. If mode cannot be determined for given type and file name not <code>null</code>, then an
  * attempt will be tried for a type defined for this name. Method returns <code>null</code> if
  * mode cannot be determined from given parameters.
  *
  * @param type {@link String} a MIME type string or <code>null</code>
  * @param name {@link String} a file name or <code>null</code>
  * @return {@link String} with UI mode for given MIME type or <code>null</code> if mode cannot be
  *     determined
  */
 public String getMimeTypeMode(String type, String name) {
   if (type == null && name != null) {
     type = getMimeType(name);
   }
   if (type != null) {
     try {
       boolean tryResolved;
       do {
         tryResolved = false;
         MimeType mimeType = new MimeType(type);
         String mode = mimeType.getParameter(ExtendedMimeTypeResolver.X_TYPE_MODE);
         if (mode == null || mode.length() == 0) {
           // try in this resolved map
           Set<String> modeList = modes.get(mimeType.getBaseType());
           if (modeList != null && modeList.size() == 1) {
             // if have one-to-one relation - OK, else will try by filename
             return modeList.iterator().next();
           } else if (name != null) {
             // try with a type resolved from given filename
             type = getMimeType(name);
             tryResolved = true;
             name = null; // null to do not repeat this attempt
           } else if (modeList != null && modeList.size() > 0) {
             // worse case: we have several modes for given type and need choose... first one
             return modeList.iterator().next();
           }
         } else {
           return mode;
         }
       } while (type != null && tryResolved);
     } catch (MimeTypeParseException e) {
       LOG.warn("Error parsing mimetype " + type + ": " + e.getMessage());
     }
   }
   return null;
 }