Linking to javadoc.io using Javadoc -link option

前端 未结 5 1326
野的像风
野的像风 2021-02-07 15:29

I am trying to link into some Javadocs hosted at javadoc.io (specifically, PowerMock\'s Javadocs) using the @link option. I have tried to add the URL to PowerMock\'

5条回答
  •  时光说笑
    2021-02-07 16:10

    From the command line, use an argument like -J-Dhttp.agent=javadoc.

    In Maven, use something like:

    -J-Dhttp.agent=maven-javadoc-plugin-${pom‌​.name}

    The background: As Danilo Pianini suggests in another answer, the problem is the User-Agent header. However, the problem isn't an empty User-Agent; it's the default Java User-Agent, which looks something like "Java/1.8.0_112":

    $ URL=https://static.javadoc.io/org.checkerframework/checker-qual/2.2.2/package-list
    
    # default Java User-Agent:
    $ wget -U Java/1.8.0_112 "$URL" 2>&1 | grep response
    HTTP request sent, awaiting response... 403 Forbidden
    
    # no User-Agent:
    $ wget -U '' "$URL" 2>&1 | grep response
    HTTP request sent, awaiting response... 200 OK
    
    # custom User-Agent:
    $ wget -U javadoc "$URL" 2>&1 | grep response
    HTTP request sent, awaiting response... 200 OK
    

    So the fix is to tell Javadoc to use a different User-Agent. Java won't let you omit the User-Agent, so you'll have to provide a value, which Java will prepend to its default agent.

    As best I can tell, the blocking of Javadoc isn't intentional: Javadoc just (probably unwisely) uses the default Java User-Agent, and the content delivery network that javadoc.io uses blocks that by default.

    (One more note about Maven: Everything works fine with -link. It also works fine with -linkoffline if you download the package-list file and tell Javadoc to read it from disk. However, if you use -linkoffline but tell Javadoc to fetch package-list from the javadoc.io URL (this is an unusual thing to do), it may fail. The problem: Maven tries to pre-validate the package-list file but, under some versions of Java, fails because it rejects the SSL certificate of javadoc.io, a certificate that Javadoc itself accepts.)

    (Oh, and it appears to be important to use a URL specifically from static.javadoc.io, not javadoc.io. Also, I would recommend https, not http, in case http://static.javadoc.io someday starts issuing redirects to https://static.javadoc.io, as Javadoc currently doesn't handle such redirects. Also, https is a good thing :))

提交回复
热议问题