Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

"The method ... is ambiguous for ..."

Thread view: 
Ian Wilson - 10 May 2007 15:29 GMT
I'm getting an error message I don't understand.

The message is "The method newCanonicalizationMethod(String,
C14NMethodParameterSpec) is ambiguous for the type XMLSignatureFactory"

The code is "Code Fragment 2" from
http://java.sun.com/developer/technicalArticles/xml/dig_signatures/

Which I cut and pasted into a new class in Eclipse WST. I then used
Eclipse' Ctrl+O to generate the includes, where the class was available
in several packages, I picked the packages mentioned earlier in the
article (see below for full code)

My plan was use the error messages to guide me in adding suitable code
to create any undefined variables etc.

When I look at the API docs for XMLSignatureFactory I only see one
method named newCanonicalizationMethod so I can't see how it can be
ambiguous. It looks OK to me.

http://java.sun.com/webservices/docs/1.4/api/javax/xml/crypto/dsig/XMLSignatureF
actory.html


Why is it ambiguous and what can I do about it?

--------------------------8<----------------------------------------
import java.util.ArrayList;
import java.util.Collections;

import javax.xml.crypto.dom.DOMStructure;
import javax.xml.crypto.dsig.CanonicalizationMethod;
import javax.xml.crypto.dsig.DigestMethod;
import javax.xml.crypto.dsig.Reference;
import javax.xml.crypto.dsig.SignatureMethod;
import javax.xml.crypto.dsig.SignedInfo;
import javax.xml.crypto.dsig.XMLSignature;
import javax.xml.crypto.dsig.XMLSignatureFactory;
import javax.xml.crypto.dsig.dom.DOMSignContext;
import javax.xml.crypto.dsig.keyinfo.KeyInfo;
import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;

public class IWSignXML {
    public static void main(String[] args) {

      XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
      DigestMethod digestMethod = fac.newDigestMethod
              ("http://www.w3.org/2000/09/xmldsig#sha1", null);
      Reference ref = fac.newReference("#10",digestMethod);
      ArrayList<Reference> refList = new ArrayList<Reference>();
      refList.add(ref);
      CanonicalizationMethod cm =  fac.newCanonicalizationMethod(
              "http://www.w3.org/2001/10/xml-exc-c14n#",null);
      SignatureMethod sm = fac.newSignatureMethod(
              "http://www.w3.org/2000/09/xmldsig#rsa-sha1",null);
      SignedInfo signedInfo =fac.newSignedInfo(cm,sm,refList);
      DOMSignContext signContext = null;
      signContext = new DOMSignContext(privKey,securityHeader);
      signContext.setURIDereferencer(new URIResolverImpl());
      KeyInfoFactory keyFactory = KeyInfoFactory.getInstance();
      DOMStructure domKeyInfo = new DOMStructure(tokenReference);
      KeyInfo keyInfo =
          keyFactory.newKeyInfo(Collections.singletonList(domKeyInfo));
      XMLSignature signature = fac.newXMLSignature(signedInfo,keyInfo);
      signature.sign(signContext);

    }
}
---------------------------------------------------------------------

P.S. This is a bit of an X Y problem. The error message is Y, the X is
how to read an XML document and insert a digital signature of a part of
it (the Body element of a SOAP message).

P.P.S. Its actually X Y Z, the Z being that I want to check my Perl code
is canonicalising XML correctly by comparing its output with some SHA-1
digests produced by Java. If I could canonicalize XML and compute an
SHA-1 digest I'd be happy.
Ian Wilson - 10 May 2007 18:41 GMT
> I'm getting an error message I don't understand.
>
[quoted text clipped - 6 lines]
>       CanonicalizationMethod cm =  fac.newCanonicalizationMethod(
>               "http://www.w3.org/2001/10/xml-exc-c14n#",null);

By using the Sun command line compiler (javac) instead of Eclipse, I got
a more explicit error message:

"IWSignXML.java:25: reference to newCanonicalizationMethod is ambiguous,
both method newCanonicalizationMethod
(java.lang.String,javax.xml.crypto.dsig.spec.C14NMethodParameterSpec) in
javax.xml.crypto.dsig.XMLSignatureFactory and method
newCanonicalizationMethod
(java.lang.String,javax.xml.crypto.XMLStructure) in
javax.xml.crypto.dsig.XMLSignatureFactory match"

The cause was the use of null as a second parameter (which matches any
object) combined with the existence of another method with the same name
and a different two element signature which wasn't in the JavaDocs I was
using.

I changed the above code to
      C14NMethodParameterSpec nullspec = null;
      CanonicalizationMethod cm =  fac.newCanonicalizationMethod(
              "http://www.w3.org/2001/10/xml-exc-c14n#",nullspec);

And the compiler was happy.
Oliver Wong - 10 May 2007 21:54 GMT
[...]
> The cause was the use of null as a second parameter (which matches any
> object) combined with the existence of another method with the same name
[quoted text clipped - 7 lines]
>
> And the compiler was happy.

   Perhaps you're already aware of this trick, but the fix I usually use
for cases like these is to cast the null:

CanonicalizationMethod cm =  fac.newCanonicalizationMethod(
               "http://www.w3.org/2001/10/xml-exc-c14n#",(C14NMethodParameterSpec)null);

   - Oliver
Ian Wilson - 11 May 2007 09:36 GMT
> [...]
>
[quoted text clipped - 15 lines]
> CanonicalizationMethod cm =  fac.newCanonicalizationMethod(
>                 "http://www.w3.org/2001/10/xml-exc-c14n#",(C14NMethodParameterSpec)null);

I wasn't already aware of that trick. Thanks for passing it on.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.