Package org.openxri.xml

Examples of org.openxri.xml.XRDS


    public XRD resolveAuthToXRD(XRI qxri, TrustType trustType, boolean followRefs, ResolverState state)
    throws PartialResolutionException
  {
    log.trace("resolveAuthToXRD('" + qxri + "', trustType=" + trustType + ", followRefs=" + followRefs + ")");
   
    XRDS xrds = null;
      if (proxyURI != null)
        xrds = resolveViaProxy(qxri, trustType, null, null, false, followRefs, state);
      else
        xrds = resolveAuthToXRDS(qxri, trustType, followRefs, state);
    return xrds.getFinalXRD();
  }
View Full Code Here


    throws PartialResolutionException
  {
    log.trace("resolveSEPToXRDS('" + qxri + "', trustType=" + trustType + ", sepType=" + sepType
        + ", sepMediaType=" + sepMediaType + ", followRefs=" + followRefs + ")");
 
    XRDS xrds = resolveAuthToXRDS(qxri, trustType, followRefs, state);
    XRD finalXRD = xrds.getFinalXRD();
   
    XRDS resultXRDS = null;
    try {
      resultXRDS = selectServiceFromXRD(finalXRD, qxri, trustType, sepType, sepMediaType, followRefs, state);
    }
    catch (PartialResolutionException e) {
      xrds.replaceFinalXRD(e.getPartialXRDS());
View Full Code Here

    throws PartialResolutionException
  {
    log.trace("resolveSEPToXRD('" + qxri + "', trustType=" + trustType + ", sepType=" + sepType
        + ", sepMediaType=" + sepMediaType + ", followRefs=" + followRefs + ")");
   
    XRDS xrds = resolveSEPToXRDS(qxri, trustType, sepType, sepMediaType, followRefs, state);
    return xrds.getFinalXRD();
  }
View Full Code Here

    public XRDS resolveAuthTop(XRI qxri, TrustType trustType, boolean followRefs, ResolverState state)
      throws PartialResolutionException
    {
      log.trace("resolveAuthTop('" + qxri + "', trustType=" + trustType + ", followRefs=" + followRefs + ")");

      XRDS xrdsOut = new XRDS();
      xrdsOut.setRef("xri://" + qxri.getAuthorityPath().toString());
     
      // get the authority
      AuthorityPath ap = qxri.getAuthorityPath();
      if (ap instanceof XRIAuthority) {
        String rootAuth = ((XRIAuthority)ap).getRootAuthority();
        if (rootAuth == null) {
          throw new RuntimeException("First subsegment of '" + ap + "' is null");
        }
       
        XRD authXRD = getAuthority(rootAuth);
        if (authXRD == null) {
          // unknown root
          XRD err = createErrorXRD(qxri.toURINormalForm(), Status.UNKNOWN_ROOT, "Authority '" + rootAuth + "' is not configured");
          xrdsOut.add(err);
          throw new PartialResolutionException(xrdsOut);
        }
       
        XRISegment unresolved = ((XRIAuthority)ap).getXRISegment();
        try {
          XRDS newXRDS = resolveAuthSegment(authXRD, unresolved, trustType, followRefs, state);
          xrdsOut.addAll(newXRDS);
            return xrdsOut;
        }
        catch (PartialResolutionException e) {
          xrdsOut.addAll(e.getPartialXRDS());
          throw new PartialResolutionException(xrdsOut);
        }
      }
      else if (ap instanceof IRIAuthority) {
        try {
          XRDS newXRDS = resolveIRIAuth((IRIAuthority)ap, trustType, followRefs, state);
          xrdsOut.addAll(newXRDS);
          return xrdsOut;
        }
        catch (PartialResolutionException e) {
          xrdsOut.addAll(e.getPartialXRDS());
View Full Code Here

    protected XRDS resolveIRIAuth(IRIAuthority iriAuth, TrustType trustType, boolean followRefs, ResolverState state)
      throws PartialResolutionException
    {
      log.trace("resolveIRIAuth('" + iriAuth + "', trustType=" + trustType + ", followRefs=" + followRefs + ")");

      XRDS xrdsOut = new XRDS();
     
      if (trustType.isSAML()) {
        XRD err = createErrorXRD(iriAuth.toURINormalForm(), Status.NOT_IMPLEMENTED, "SAML is not supported for an IRI authority");
        xrdsOut.add(err);
        throw new PartialResolutionException(xrdsOut);
      }
     
        // only use http for insecure and https for secure
        String scheme = trustType.isHTTPS() ? "https" : "http";

        URI uri = null;
        try
        {
            uri =
                new URI(
                    scheme, iriAuth.getIUserInfo(), iriAuth.getIHost(),
                    iriAuth.getPort(), null, null, null);
        }
        catch (java.net.URISyntaxException e)
        {
          XRD err = createErrorXRD(iriAuth.toURINormalForm(), Status.INVALID_INPUT, "Unable to construct URI to resolve IRI authority: " + e.getMessage());
          xrdsOut.add(err);
          throw new PartialResolutionException(xrdsOut);
        }

        // now that we've constructed the new URI, try to return the stream from it
        InputStream in = null;
        try {
          in = getDataFromURI(uri.toString(), uri, trustType, state);
        }
        catch (Exception e) {
          XRD err = createErrorXRD(iriAuth.toURINormalForm(), Status.NETWORK_ERROR, "Network error occurred while resolving IRI authority: " + e.getMessage());
          xrdsOut.add(err);
          throw new PartialResolutionException(xrdsOut);
        }

        if (in == null) {
          throw new RuntimeException("resolveIRIAuth - getDataFromURI returned null");
        }
       
        // read the descriptors
        try {
          xrdsOut = readXRDS(in);
            if (xrdsOut.getNumChildren() != 1) {
              XRD err = createErrorXRD(iriAuth.toURINormalForm(), Status.UNEXPECTED_RESPONSE, "Expected 1 XRD from IRI authority, got " + xrdsOut.getNumChildren() + " instead");
              xrdsOut.add(err);
              throw new PartialResolutionException(xrdsOut);
            }
        }
        catch (XRIResolutionException e) {
          XRD err = createErrorXRD(iriAuth.toURINormalForm(), Status.UNEXPECTED_RESPONSE, "Error reading XRDS from server: " + e.getMessage());
          xrdsOut.add(err);
          throw new PartialResolutionException(xrdsOut);         
        }

        // add the descriptor, but only if is is valid
        XRD xrd = xrdsOut.getDescriptorAt(0);
        if (!xrd.isValid())
        {
          XRD err = createErrorXRD(iriAuth.toURINormalForm(), Status.UNEXPECTED_XRD, "XRD is invalid");
          xrdsOut.add(err);
          throw new PartialResolutionException(xrdsOut);
        }

        return xrdsOut;
    }
View Full Code Here

        throws PartialResolutionException
    {
      log.trace("resolveViaProxy('" + qxri + "', trustType=" + trustType + ", serviceType=" + serviceType +
          ", serviceMediaType=" + serviceMediaType + ", followRefs=" + followRefs + ")");

      XRDS xrdsOut = new XRDS();
     
        // build the new URI for the proxy
        URI newURI = null;
        try
        {
          StringBuffer query = new StringBuffer();
          if (serviceType != null) {
            query.append("_xrd_t=");
            query.append(URLEncoder.encode(serviceType, "UTF-8"));
            query.append('&');
          }
         
          if (serviceMediaType != null) {
            query.append("_xrd_m=");
            query.append(URLEncoder.encode(serviceMediaType, "UTF-8"));
            query.append('&');
          }
         
          query.append("_xrd_r=");
          query.append(Tags.CONTENT_TYPE_XRDS);
          query.append(';');
          query.append(trustType.getParameterPair());
          query.append(";sep=");
          query.append(sepSelect);
          query.append(";ref=");
          query.append(followRefs);
          query.append('&');

          if (proxyURI.getQuery() != null) {
            query.append(proxyURI.getQuery());
          }
         
          StringBuffer uriBuf = new StringBuffer();
          uriBuf.append(proxyURI.getScheme());
          uriBuf.append("://");
          uriBuf.append(proxyURI.getAuthority());
          uriBuf.append(proxyURI.getPath());
          if (uriBuf.charAt(uriBuf.length() - 1) != '/')
            uriBuf.append('/');
         
          uriBuf.append(qxri.getAuthorityPath().toURINormalForm());
          uriBuf.append('?');
          uriBuf.append(query);

          log.trace("resolveViaProxy - constructed proxy query URI '" + uriBuf + "'");

          newURI = new URI(uriBuf.toString());
        }
        catch (java.net.URISyntaxException oEx)
        {
          XRD err = createErrorXRD(qxri.getAuthorityPath().toURINormalForm(),
            Status.INVALID_INPUT, "Unable to construct URI to access proxy resolution service");
          xrdsOut.add(err);
          throw new PartialResolutionException(xrdsOut);
        }
        catch (UnsupportedEncodingException e)
        {
          // thrown from URLEncoder.encode() - this should never happen since the
          // US-ASCII encoding should be supported on every computer or so we hope :)
          XRD err = createErrorXRD(qxri.getAuthorityPath().toURINormalForm(),
                Status.INVALID_INPUT, "Charset not supported");
              xrdsOut.add(err);
              throw new PartialResolutionException(xrdsOut);
        }
        catch (Exception e) {
          XRD err = createErrorXRD(qxri.getAuthorityPath().toURINormalForm(),
                Status.PERM_FAIL, "Unexpected error while constructing proxy URI: " + e.getMessage());
              xrdsOut.add(err);
              throw new PartialResolutionException(xrdsOut);
        }

        InputStream in = null;
        try {
            // try to get the data from it
            in = getDataFromURI(qxri.toURINormalForm(), newURI, trustType, state);
            XRDS xrds = readXRDS(in);
            XRD finalXRD = xrds.getFinalXRD();

            String code = finalXRD.getStatusCode();
            if ((followRefs && !code.equals(Status.SUCCESS) && !code.equals(Status.REF_NOT_FOLLOWED))
                || !code.equals(Status.SUCCESS))
            {
View Full Code Here

     * @throws PartialResolutionException
     */
    protected XRDS processAuthRefs(ArrayList servicesOut, XRD parent, XRISegment segment, TrustType trustType, boolean followRefs, ResolverState state)
      throws PartialResolutionException
    {
      XRDS xrdsOut = new XRDS();
     
      ///// Try finding the auth res service
      ///// get the authority resolution SEP URIs in parent
      ArrayList selectedServices = selectServices(parent.getServices(),
          Tags.SERVICE_AUTH_RES, null, Tags.CONTENT_TYPE_XRDS + ";" + trustType.getParameterPair());
     
      if (selectedServices.size() > 0) {
        ///// found at least one $res*auth service, return what we have so far
        servicesOut.addAll(selectedServices);
        return xrdsOut;
      }

      ///// no service selected, follow refs
    ArrayList refs = parent.getPrioritizedRefs();
   
    if (refs.size() < 1) {
      XRD err = createErrorXRD(segment.getSubSegmentAt(0).toURINormalForm(),
        Status.AUTH_RES_ERROR, "Requested service endpoint not found");
      xrdsOut.add(err);
      throw new PartialResolutionException(xrdsOut);
    }
    else if (!followRefs) {
      // there are Refs in the current XRDS but we were told not to follow any
      XRD err = createErrorXRD(segment.getSubSegmentAt(0).toURINormalForm(),
        Status.REF_NOT_FOLLOWED, "References not followed");
      xrdsOut.add(err);
      throw new PartialResolutionException(xrdsOut);
    }

    if (maxFollowRefs >= 0 && state.getNumRefsFollowed() >= maxFollowRefs) {
      XRD err = createErrorXRD(segment.getSubSegmentAt(0).toURINormalForm(),
        Status.LIMIT_EXCEEDED, "Maximum number of references exceeded");
      xrdsOut.add(err);
      throw new PartialResolutionException(xrdsOut);
    }
   

    ///// attempt each ref in turn
    for (int i = 0; i < refs.size(); i++) {
      Ref r = (Ref)refs.get(i);
      XRI refXRI = null;
      try {
        refXRI = parseAbsoluteQXRIOrError(r.getValue());
      }
      catch (PartialResolutionException e) {
        log.warn("resolveAuthSegment - Invalid ref encountered: " + e.getMessage());
        continue;
      }

      state.pushFollowingRef(refXRI);
     
      // at this point we will not try another ref; if this fails we will bail out
      XRDS xrds = null;
      try {
        xrds = resolveAuthTop(refXRI, trustType, followRefs, state);
          xrdsOut.add(xrds);
      }
      catch (PartialResolutionException e) {
        // add the resulting XRDS from the exception to ours and re-throw
        xrdsOut.add(e.getPartialXRDS());
        throw new PartialResolutionException(xrdsOut);
      }

      try {
          XRDS xrds2 = processAuthRefs(servicesOut, xrds.getFinalXRD(), segment, trustType, followRefs, state);
          if (xrds2.getNumChildren() > 0)
            xrds.addAll(xrds2);
          return xrdsOut;
      }
      catch (PartialResolutionException e) {
        // add the resulting XRDS from the exception to ours and re-throw
View Full Code Here

    flags.setNoDefaultM(nodefault_m);
    flags.setNoDefaultP(nodefault_p);
    flags.setNoDefaultT(nodefault_t);
   
    // set the request preferences on the resolver before querying.
    XRDS xrds = null;
    XRD xrd = null;
    try
    {
      if (sep) {
        if (resMediaType == null) {
          // see section 7.6 for special redirect rule
//          ArrayList<?> uris = this.resolver.resolveSEPToURIList(oXRI.toString(), trustType, serviceType, serviceMediaType, refs);
          ArrayList<?> uris = this.resolver.resolveSEPToURIList(oXRI, serviceType, serviceMediaType, flags, state);
          if (uris == null || uris.size() == 0) {
            sendResponse(response, HTTP_ERROR_CONTENT_TYPE, "SEP_NOT_FOUND(code=241): no url found", null);
            return;
          }
          String s = (String) uris.get(0);

          log.trace("Sending redirect to '" + s + "'");

          if (this.config.getSetting("RedirectCode").equals("301"))
            send301(response, s);
          else if (this.config.getSetting("RedirectCode").equals("303"))
            send303(response, s);
          else
            response.sendRedirect(s);
        }
        else if (resMediaType.isType(MimeType.URI_LIST)) {
//          String  text = this.resolver.resolveSEPToTextURIList(oXRI.toString(), trustType, serviceType, serviceMediaType, refs);
          String text = this.resolver.resolveSEPToTextURIList(oXRI, serviceType, serviceMediaType, flags, state);
          if (text.length() <= 0)
            sendResponse(response, HTTP_ERROR_CONTENT_TYPE, "SEP_NOT_FOUND(code=241): no url found", null);
          else
            sendResponse(response, resMediaType.getType(), text, null);
        }
        else if (resMediaType.isType(MimeType.XRDS_XML)) {
//          xrds = this.resolver.resolveSEPToXRDS(oXRI, trustType, serviceType, serviceMediaType, refs);
          xrds = this.resolver.resolveSEPToXRDS(oXRI, serviceType, serviceMediaType, flags, state);
          sendResponse(response, debug, resMediaType.getType(), xrds.toString(), trustType);
        }
        else if (resMediaType.isType(MimeType.XRD_XML)) {
//          xrd = this.resolver.resolveSEPToXRD(oXRI, trustType, serviceType, serviceMediaType, refs);
          xrd = this.resolver.resolveSEPToXRD(oXRI, serviceType, serviceMediaType, flags, state);
          sendResponse(response, debug, resMediaType.getType(), xrd.toResultString(), trustType);
        }
        else {
          // else - we should have taken care of it in checkSupportedMediaTypes
          log.error("processProxyRequest - should not reach here (sep=true)");
        }
      }
      else {
        //// authority resolution only
        if(resMediaType == null) {
          resMediaType = new MimeType(MimeType.XRDS_XML);
        }

        if (resMediaType.isType(MimeType.XRDS_XML)) {
//          xrds = this.resolver.resolveAuthToXRDS(oXRI, trustType, refs);
          xrds = this.resolver.resolveAuthToXRDS(oXRI, flags, state);
          sendResponse(response, debug, resMediaType.getType(), xrds.toString(), trustType);
          return;
        }
        else if (resMediaType.isType(MimeType.XRD_XML)) {
//          xrd = this.resolver.resolveAuthToXRD(oXRI, trustType, refs);
          xrd = this.resolver.resolveAuthToXRD(oXRI, flags, state);
View Full Code Here

    {
      log.trace("resolveAuthSegment - segment='" + segment + "'");

      ArrayList selectedServices = new ArrayList();
     
      XRDS xrdsOut = processAuthRefs(selectedServices, parent, segment, trustType, followRefs, state);
     
      if (maxRequests >= 0 && state.getNumRequests() >= maxRequests) {
        XRD finalXRD = xrdsOut.getFinalXRD();
        if (finalXRD == null) {
          finalXRD = createErrorXRD(segment.toURINormalForm(true), Status.LIMIT_EXCEEDED, "Maximum of authority resolution requests exceeded");
          xrdsOut.add(finalXRD);
        }
        else {
          finalXRD.setStatus(new Status(Status.LIMIT_EXCEEDED, "Maximum of authority resolution requests exceeded"));
        }
        throw new PartialResolutionException(xrdsOut);       
      }
     
      ///// Try each URI in each selected service in turn
      Exception savedException = null;
      Iterator srvIterator = selectedServices.iterator();
      while (srvIterator.hasNext()) {
        Service srv = (Service)srvIterator.next();
       
        Iterator uriIterator = srv.getPrioritizedURIs().iterator();
          ///// try each selected service URI in turn (skip only if nothing was read)
          while (uriIterator.hasNext()) {
            SEPUri sepURI = (SEPUri)uriIterator.next();
            URI uri = sepURI.getURI();
           
            log.trace("resolveAuthSegment - trying URI='" + uri + "'");

            // skip non-HTTPS URIs if HTTPS was requested
            if (trustType.isHTTPS() && !uri.getScheme().equals(HTTPS)) {
              log.trace("resolveAuthSegment - skipping non HTTPS URI");
              continue;
            }
           
            URI newURI;
                try
                {
                    newURI = constructAuthResURI(uri.toString(), segment.toURINormalForm(true));               
                    log.trace("resolveAuthSegment - newURI = " + newURI);
                }
                catch (java.net.URISyntaxException oEx)
                {
                    // oops! invalid authority URI
                  savedException = new InvalidAuthorityURIException(
                        "Could not create URI to access based on " + uri +
                        ".  Trying to resolve " + segment, oEx);
                  continue; // try next URI
                }

                XRDS newXRDS = null;
                // now that we've constructed the new URI, try to return the stream from it
                try {
                  InputStream in = getDataFromURI(segment.toString(), newURI, trustType, state);
                    newXRDS = readXRDS(in);
                    log.debug("Got XRDS = " + newXRDS.toString());
                }
                catch (Exception e) {
                  log.trace("resolveAuthSegment - bad URI");
                  savedException = e;
                  continue;
                }
               
                // set ourselves up for the next multi-pass
                for (int d = 0; d < newXRDS.getNumChildren() && d < segment.getNumSubSegments(); d++)
                {
                    XRD xrd = newXRDS.getDescriptorAt(d);
                   
                    // status is not success
                    Status stat = xrd.getStatus();
                    if (stat == null) {
                      xrd = createErrorXRD(segment.getSubSegmentAt(d).toString(),
                        Status.UNEXPECTED_XRD, "Status code was missing in original XRD");
                    }
                   
                    // check the basic properties of the descriptor
                    boolean bValid = xrd.isValid();
                    if (!bValid) {
                      xrd = createErrorXRD(segment.getSubSegmentAt(d).toString(),
                        Status.UNEXPECTED_XRD, "Invalid XRD (stale?) received");
                    }

                    /*
                    // if we need to do trusted resolution checking
                    if (trustType.isSAML())
                    {
                        // Each descriptor must be validated independently as well as
                        // against the one that preceded (described) it in the
                        // descriptor chain.
                      // TODO: there could be more than one Authority Resolution Service
                      //       in the final XRD
                        bValid =
                            isTrustedDescriptor(
                                oAuth.getSubSegmentAt(iSubSeg), xrd,
                                oChain.getFinalXRIDescriptor().getFirstServiceByType(Tags.SERVICE_AUTH_RES));

                        // bail if the descriptor is not valid
                        if (!bValid)
                          throw new XRIResolutionException("Signature verification failed");

                    }
                    */

                    xrdsOut.add(xrd);
                   
                    if (!xrd.getStatus().getCode().equals(Status.SUCCESS)) {
                      throw new PartialResolutionException(xrdsOut);
                    }
                }

                // in case we are not able to parse the XRDS, or no XRD in there
                if (newXRDS.getNumChildren() < 1) {
                  XRD xrd = createErrorXRD(segment.getSubSegmentAt(0).toString(),
                        Status.AUTH_RES_ERROR, "No XRD element returned from endpoint");
                  xrdsOut.add(xrd);
                  throw new PartialResolutionException(xrdsOut);
                }
               
                if (newXRDS.getNumChildren() >= segment.getNumSubSegments()) {
                  // we're done!
                  return xrdsOut;
                }
               
                // not done yet, recursively resolve
               
                XRISegment remainder = segment.getRemainder(newXRDS.getNumChildren());
                XRDS remainderXRDS = null;
               
                try {
                  remainderXRDS = resolveAuthSegment(newXRDS.getDescriptorAt(newXRDS.getNumChildren()-1), remainder, trustType, followRefs, state);
                }
                catch (PartialResolutionException e) {
View Full Code Here

          String sepMediaType,
          boolean followRefs,
          ResolverState state
    ) throws PartialResolutionException
    {
      XRDS xrdsOut = new XRDS();
     
      // get the QXRI path
      String path = null;
      XRIAbsolutePath absPath = qxri.getXRIAbsolutePath();
      if (absPath != null)
        path = absPath.toURINormalForm();
      if (path != null && path.length() > 1 && path.charAt(0) == '/') {
        path = path.substring(1); // remove the leading slash
      }

      xrdsOut.add(xrd);
     
      // find services
      ArrayList selectedSvcs = selectServices(xrd.getServices(), sepType, path, sepMediaType);
      if (selectedSvcs.size() == 0) {
        // do reference processing
        ArrayList refs = xrd.getPrioritizedRefs();
        if (refs.size() == 0) {
          Status s = new Status(Status.SEP_NOT_FOUND, "Requested service endpoint not found.");
          xrdsOut.getFinalXRD().setStatus(s);
          throw new PartialResolutionException(xrdsOut);
        }
       
        if (maxFollowRefs >= 0 && state.getNumRefsFollowed() >= maxFollowRefs) {
          Status s = new Status(Status.LIMIT_EXCEEDED, "Maximum number of references exceeded");
          xrdsOut.getFinalXRD().setStatus(s);
          throw new PartialResolutionException(xrdsOut);         
        }

        // we have some references, let's process it
        for (int i = 0; i < refs.size(); i++) {
          Ref r = (Ref)refs.get(i);
          XRI refXRI = null;
          try {
            refXRI = new XRI(r.getValue());
          }
          catch (XRIParseException e) {
            log.warn("resolveAuthSegment - Invalid ref encountered: " + e.getMessage());
           
            // bail out if this is the last segment
            if (i == refs.size()) {
                Status s = new Status(Status.SEP_NOT_FOUND, "Invalid ref encountered: " + e.getMessage());
                xrdsOut.getFinalXRD().setStatus(s);
                throw new PartialResolutionException(xrdsOut);
            }
            else // try next one
              continue;
          }

          state.pushFollowingRef(refXRI);
         
          XRDS refXRDS = null;
          try {
            refXRDS = resolveAuthTop(refXRI, trustType, followRefs, state);
          }
          catch (PartialResolutionException e) {
            xrdsOut.addAll(e.getPartialXRDS());
            throw new PartialResolutionException(xrdsOut);
          }
         
          xrdsOut.add(refXRDS);

          // ref processing completed
          // recursively select service
          XRDS resultXRDS = null;
          try {
              resultXRDS = selectServiceFromXRD(refXRDS.getFinalXRD(), qxri, trustType, sepType, sepMediaType, followRefs, state);
          }
          catch (PartialResolutionException e) {
            xrdsOut.replaceFinalXRD(e.getPartialXRDS());
View Full Code Here

TOP

Related Classes of org.openxri.xml.XRDS

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.