Coder Social home page Coder Social logo

urlbuilder's Introduction

Java URL builder

Build Status Coverage Status Maven Central

Create and modify URLs and URL parameters easily, with a builder class.

Builder instances are immutable, thread-safe and reusable. Every change creates a new instance.

UrlBuilder.fromString("http://www.google.com/")
    .addParameter("q", "charlie brown")
    .toString() == "http://www.google.com/?q=charlie+brown"

UrlBuilder.fromString("http://foo/h%F6pl%E4", "ISO-8859-1")
    .encodeAs("UTF-8")
    .toString() == "http://foo/h%C3%B6pl%C3%A4"

final UrlBuilder ub1 = UrlBuilder.empty()
    .withScheme("http")
    .withHost("www.example.com")
    .withPath("/")
    .addParameter("foo", "bar");

final java.net.URI uri1 = ub1.toUri();

try {
    final java.net.URI uri2 = ub1.toUriWithException();
} catch (java.net.URISyntaxException ex) {
    // handle the exception
}

final java.net.URL url1 = ub1.toUrl();

try {
    final java.net.URL url2 = ub1.toUrlWithException();
} catch (java.net.MalformedURLException ex) {
    // handle the exception
}

Todo:

  • More unit tests for corner cases. Please send in pull requests, your help is needed.

Use with Gradle:

implementation("io.mikael:urlbuilder:2.0.9")
implementation "io.mikael:urlbuilder:2.0.9"

Use with Maven:

<dependencies>
    <dependency>
        <groupId>io.mikael</groupId>
        <artifactId>urlbuilder</artifactId>
        <version>2.0.9</version>
    </dependency>
</dependencies>

urlbuilder's People

Contributors

arturdryomov avatar barrypitman avatar christerf avatar ekagumi avatar elpicador avatar entea avatar matdurand avatar mfulgo avatar mikaelhg avatar orip avatar siepkes avatar stevemulligan avatar yarixxx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

urlbuilder's Issues

Does this library has max string limit??

I am using this library for my maven project. The issue is that sometimes URI generated by this library does not include last 1-3 characters from string that I passed to build URI.

Mixing escaped and yet-not-escaped chars in URL strings throws an exception while create an URI object

consider Google Translate links:

http://translate.google.com/translate?hl=auto&langpair=auto|zh-TW&u=http%3A%2F%2Fus6.campaign-archive1.com%2F%3Fu%3Dcbc96c3d7a%26id%3D17fdad33a4%26e%3D
and
http://translate.google.com/translate?hl=auto&langpair=auto%7Czh-TW&u=http%3A%2F%2Fus6.campaign-archive1.com%2F%3Fu%3Dcbc96c3d7a%26id%3D17fdad33a4%26e%3D

the first one results in

Illegal character in query at index 59: http://translate.google.com/translate?hl=auto&langpair=auto|zh-TW&u=http%3A%2F%2Fus6.campaign-archive1.com%2F%3Fu%3Dcbc96c3d7a%26id%3D17fdad33a4%26e%3D

the second one works just fine

the problem here is the pipe character | which is not escaped

perhaps the case when an URL contains both escaped and unescaped characters should be processed in certain way

Maven repo down

Hey there,

Looks like the maven repo is down.

maven2.gueck.com-releases http://maven2.gueck.com/releases

Can't resolve maven2.gueck.com on my local resolver. When querying the DNS server listed in the whois, I get:

; QUESTION SECTION:
;maven2.gueck.com. IN A

;; ANSWER SECTION:
maven2.gueck.com. 300 IN CNAME maven2.gueck.com.s3-website-eu-west-1.amazonaws.com.

When I try to curl, I get:

ernst@server:~$ curl -H "Host: maven2.gueck.com" maven2.gueck.com.s3-website-eu-west-1.amazonaws.com

<title>404 Not Found</title>

404 Not Found

  • Code: NoSuchKey
  • Message: The specified key does not exist.
  • Key: index.html
  • RequestId: D265A283D3F882CF
  • HostId: WieqmIq7zvhHcqNVgQbZ0paOljjZEHM+Yy8bSbUt46Eof9VspKuboaOcbqjCL7pF

An Error Occurred While Attempting to Retrieve a Custom Error Document

  • Code: NoSuchKey
  • Message: The specified key does not exist.
  • Key: error.html

Plus sign in path portion of URL gets encoded - while still being allowed

I just want to start off by saying thanks for publishing this project - there is definitely a need for a non-framework attached url builder module.

According to RFC3986 the plus (+) character is allowed as a character in the path portion of the URL, but according to the code it is being explicitly encoded.

This seems to be a deliberate omission, is there a particular reason for that?

This is a difference to how Jersey's URIBuilder is handling the same thing and making it hard to replace it with this less burdened implementation.

Maven repository 'Unknown host maven2.gueck.com'

The same as #7 problem occurs.

Could not transfer artifact mikaelhg:urlbuilder:pom:1.3.1 from/to maven2.gueck.com-releases (http://maven2.gueck.com/releases): maven2.gueck.com: Unknown host maven2.gueck.com

could you upload it to maven central?

Faulty fragment matching

When creating an UrlBuilder from an existing URL the fragment-parrt is not interpreted right

    @Test
    public void test() {
        String existingUrl = "http://localhost:5555/#/aresource?bparameter=cvalue";

        String url = UrlBuilder.fromString(existingUrl)
                .addParameter("d", "parameter")
                .toString();

        assertThat(url, is("http://localhost:5555/#/aresource?bparameter=cvalue&d=parameter"));
    }
Expected :http://localhost:5555/#/aresource?bparameter=cvalue&d=parameter
Actual   :http://localhost:5555/?d=parameter#/aresource?bparameter=cvalue

Add flag to setParameter to avoid replacing if exists

As per title,

  • Add flag to setParameter to avoid replacing if exists

Possible api:

setParameter(String key, String value, boolean overwriteExisting);
UrlBuilder.fromString("example.org?id=123").setParameter("id", defaultId, false);
// should not overwrite existing values since id has already been defined in the url

question: is there any Java or commonly used open source alternative?

Hi, and congrats for the great piece of software. In our project we try to minimize dependencies but we already have Commons Http, Guava and other commonly used Java library and we use Java 15 OpenJdk.
Would you know if there is any chance that the wonderful functionality you implemented in this library could be reconstructed in a decent way using the OpenJdk and some of the Commons / Guava libraries?
I think the answer is not really, but i will certainly be asked about that before deciding to import a new dependency.
Thank you very much

Make Encoder.encodeQueryElement public

Sometimes you need to use io.mikael.urlbuilder.util.Encoder.encodeQueryElement and I extend the Encoder to do that :) Would be great to make it public.

Auto add starting slash with withPath

I don't know if it makes any sense, or if it is the intended way the method should work, but I expected the builder to add a starting slash if my path string didn't have one.

To illustrate, I've created the following piece of code:

UrlBuilder.fromString("http://withoutslash").withPath("lol")
which outputs http://withoutslashlol . My intuition told me to expect http://withoutslash/lol.

What do you think? I can totally work on a PR for this if you decide if it's ok.

Maven repository out of service

Hi!,
I wanted to include your library in my project but the maven repo seems down, AWS is just returning 404
I think I will download compile and add to our local repository, but if you can provide a canonical one it will be better.

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.