예제 #1
0
 public static boolean mediaMatch(AcceptType t, AcceptType o) {
   try {
     String ttype = t.getMediaType().getType();
     String otype = o.getMediaType().getType();
     String tsub = t.getMediaType().getSubtype();
     String osub = o.getMediaType().getSubtype();
     List<String> tparams = t.getMediaType().getParameters();
     List<String> oparams = t.getMediaType().getParameters();
     int i;
     if (!ttype.equals(otype)) {
       return false;
     }
     if (!tsub.equals(osub)) {
       return false;
     }
     if (tparams.size() != oparams.size()) {
       return false;
     }
     for (i = 0; i < oparams.size(); i++) {
       if (!tparams.get(i).equals(oparams.get(i))) {
         return false;
       }
     }
     return true;
   } catch (NullPointerException ex) {
     return false;
   }
 }
예제 #2
0
  public static boolean isAcceptableMedia(AcceptType requested, AcceptType offerd) {
    String rType = requested.getMediaType().getType().toLowerCase();
    String oType = offerd.getMediaType().getType().toLowerCase();
    String rSub = requested.getMediaType().getSubtype();
    String oSub = offerd.getMediaType().getSubtype();

    if (rType.equals("*")) {
      return true;
    }

    if (rType.equals(oType) && rSub.equals("*")) {
      return true;
    }

    if (rType.equals(oType) && rSub.equals(oSub)) {
      return true;
    }
    return false;
  }
예제 #3
0
  public static AcceptType newInstance(String strIn) {
    Double q;
    AcceptType out = null;
    MediaType mtIn;
    MediaType mtOut = new MediaType();
    List<String> acExtList = new ArrayList<String>();
    String curParameter;
    int i;

    if (strIn == null) {
      return null;
    }
    mtIn = MediaType.newInstance(strIn);
    if (mtIn == null) {
      return out;
    }
    out = new AcceptType();
    out.setMediaType(mtOut);
    out.setAcceptExtensions(acExtList);
    mtOut.setType(mtIn.getType());
    mtOut.setSubtype(mtIn.getSubtype());
    i = 0;
    for (i = 0; i < mtIn.getParameters().size(); i++) {
      curParameter = mtIn.getParameters().get(i);
      q = AcceptType.getQfromString(curParameter);
      if (q != null) {
        out.setQ(q);
        i++;
        break;
      }
      mtOut.getParameters().add(curParameter);
    }

    for (; i < mtIn.getParameters().size(); i++) {
      acExtList.add(mtIn.getParameters().get(i));
    }
    return out;
  }
예제 #4
0
  @Override
  public int compareTo(AcceptType o) {
    // Sorts the AcceptType according to according to HttpClient preferences
    // According to Section 14.1 of the Http/1.1 protocol
    double oq = o.getQ();
    double mq = this.getQ();
    String otype = o.getMediaType().getSubtype();
    String osubtype = o.getMediaType().getSubtype();
    String mtype = this.getMediaType().getType();
    String msubtype = this.getMediaType().getSubtype();
    int mscore = 0;
    int oscore = 0;

    // If the q values are different choose the highest one
    if (mq > oq) {
      return -1;
    }
    if (mq < oq) {
      return 1;
    }

    // Try to tie break off * protocols since * should have less presedence
    // type is worth 2 points subtype is just worth 1

    mscore = ((msubtype.equals("*")) ? 0 : 1) + ((mtype.equals("*")) ? 0 : 2);
    oscore = ((osubtype.equals("*")) ? 0 : 1) + ((otype.equals("*")) ? 0 : 2);
    if (mscore != oscore) {
      return oscore - mscore;
    }

    // If you still have a tie the go by the number of parameters since they are
    // more specific as suggested by the HTTP/1.1 section 14.1

    oscore = o.getMediaType().getParameters().size();
    mscore = this.getMediaType().getParameters().size();
    return oscore - mscore;
  }