UriComponents returns IP instead of domain












0














I'm weak in network technologies and maybe you can help me. I have a simple code



HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString()).build();
UriComponents newUriComponents = UriComponentsBuilder.newInstance().scheme(uriComponents.getScheme())
.host(uriComponents.getHost()).port(uriComponents.getPort()).build();

return newUriComponents.toUriString() + request.getContextPath();


This code should return link to my server with specific path. The problem is - on product server uriComponents.getHost() returns IP instead of domain name. Domain works when I go via browser to server. I can go to
http://exmaple.com/some/one/path and want to get in answer (in JSON, there are no redirections. just get request and json answer) - http://exmaple.com/some/another/path but code which I have showed returns - http://78.54.128.98.com/some/another/path (IP address just example). So I don't know why my code returns IP but not domain name. Only what I can to say more - in my local machine I don't have any problems with it. Code returns localhost, or if i add 127.0.0.1 exmaple.com to hosts file, my code will return correct exmaple.com, no any ip










share|improve this question



























    0














    I'm weak in network technologies and maybe you can help me. I have a simple code



    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
    .getRequest();
    UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString()).build();
    UriComponents newUriComponents = UriComponentsBuilder.newInstance().scheme(uriComponents.getScheme())
    .host(uriComponents.getHost()).port(uriComponents.getPort()).build();

    return newUriComponents.toUriString() + request.getContextPath();


    This code should return link to my server with specific path. The problem is - on product server uriComponents.getHost() returns IP instead of domain name. Domain works when I go via browser to server. I can go to
    http://exmaple.com/some/one/path and want to get in answer (in JSON, there are no redirections. just get request and json answer) - http://exmaple.com/some/another/path but code which I have showed returns - http://78.54.128.98.com/some/another/path (IP address just example). So I don't know why my code returns IP but not domain name. Only what I can to say more - in my local machine I don't have any problems with it. Code returns localhost, or if i add 127.0.0.1 exmaple.com to hosts file, my code will return correct exmaple.com, no any ip










    share|improve this question

























      0












      0








      0







      I'm weak in network technologies and maybe you can help me. I have a simple code



      HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
      .getRequest();
      UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString()).build();
      UriComponents newUriComponents = UriComponentsBuilder.newInstance().scheme(uriComponents.getScheme())
      .host(uriComponents.getHost()).port(uriComponents.getPort()).build();

      return newUriComponents.toUriString() + request.getContextPath();


      This code should return link to my server with specific path. The problem is - on product server uriComponents.getHost() returns IP instead of domain name. Domain works when I go via browser to server. I can go to
      http://exmaple.com/some/one/path and want to get in answer (in JSON, there are no redirections. just get request and json answer) - http://exmaple.com/some/another/path but code which I have showed returns - http://78.54.128.98.com/some/another/path (IP address just example). So I don't know why my code returns IP but not domain name. Only what I can to say more - in my local machine I don't have any problems with it. Code returns localhost, or if i add 127.0.0.1 exmaple.com to hosts file, my code will return correct exmaple.com, no any ip










      share|improve this question













      I'm weak in network technologies and maybe you can help me. I have a simple code



      HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
      .getRequest();
      UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString()).build();
      UriComponents newUriComponents = UriComponentsBuilder.newInstance().scheme(uriComponents.getScheme())
      .host(uriComponents.getHost()).port(uriComponents.getPort()).build();

      return newUriComponents.toUriString() + request.getContextPath();


      This code should return link to my server with specific path. The problem is - on product server uriComponents.getHost() returns IP instead of domain name. Domain works when I go via browser to server. I can go to
      http://exmaple.com/some/one/path and want to get in answer (in JSON, there are no redirections. just get request and json answer) - http://exmaple.com/some/another/path but code which I have showed returns - http://78.54.128.98.com/some/another/path (IP address just example). So I don't know why my code returns IP but not domain name. Only what I can to say more - in my local machine I don't have any problems with it. Code returns localhost, or if i add 127.0.0.1 exmaple.com to hosts file, my code will return correct exmaple.com, no any ip







      java spring networking dns






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 11:16









      FoxNet

      308




      308
























          1 Answer
          1






          active

          oldest

          votes


















          0














          This is not a problem of the URIComponents, it parses what it gets in input. More specifically looking at the source of UriComponentsBuilder.fromHttpUrl you see:



          public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
          Assert.notNull(httpUrl, "HTTP URL must not be null");
          Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl);
          if (matcher.matches()) {
          UriComponentsBuilder builder = new UriComponentsBuilder();
          String scheme = matcher.group(1);
          builder.scheme(scheme != null ? scheme.toLowerCase() : null);
          builder.userInfo(matcher.group(4));
          String host = matcher.group(5);
          if (StringUtils.hasLength(scheme) && !StringUtils.hasLength(host)) {
          throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
          }
          builder.host(host);
          String port = matcher.group(7);
          if (StringUtils.hasLength(port)) {
          builder.port(port);
          }
          builder.path(matcher.group(8));
          builder.query(matcher.group(10));
          return builder;
          }
          else {
          throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
          }
          }


          where you can notice that a pattern matcher is defined on an expected structure of the url and parts are parsed according to the matcher. If you see IP it means that the url specified in input (request.getRequestURL().toString()) contained the IP address as a host.



          This means that you should be looking for the guilty one above in the chain, starting by whoever calls this piece of code and following the links until you find the cause.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410938%2furicomponents-returns-ip-instead-of-domain%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            This is not a problem of the URIComponents, it parses what it gets in input. More specifically looking at the source of UriComponentsBuilder.fromHttpUrl you see:



            public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
            Assert.notNull(httpUrl, "HTTP URL must not be null");
            Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl);
            if (matcher.matches()) {
            UriComponentsBuilder builder = new UriComponentsBuilder();
            String scheme = matcher.group(1);
            builder.scheme(scheme != null ? scheme.toLowerCase() : null);
            builder.userInfo(matcher.group(4));
            String host = matcher.group(5);
            if (StringUtils.hasLength(scheme) && !StringUtils.hasLength(host)) {
            throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
            }
            builder.host(host);
            String port = matcher.group(7);
            if (StringUtils.hasLength(port)) {
            builder.port(port);
            }
            builder.path(matcher.group(8));
            builder.query(matcher.group(10));
            return builder;
            }
            else {
            throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
            }
            }


            where you can notice that a pattern matcher is defined on an expected structure of the url and parts are parsed according to the matcher. If you see IP it means that the url specified in input (request.getRequestURL().toString()) contained the IP address as a host.



            This means that you should be looking for the guilty one above in the chain, starting by whoever calls this piece of code and following the links until you find the cause.






            share|improve this answer


























              0














              This is not a problem of the URIComponents, it parses what it gets in input. More specifically looking at the source of UriComponentsBuilder.fromHttpUrl you see:



              public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
              Assert.notNull(httpUrl, "HTTP URL must not be null");
              Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl);
              if (matcher.matches()) {
              UriComponentsBuilder builder = new UriComponentsBuilder();
              String scheme = matcher.group(1);
              builder.scheme(scheme != null ? scheme.toLowerCase() : null);
              builder.userInfo(matcher.group(4));
              String host = matcher.group(5);
              if (StringUtils.hasLength(scheme) && !StringUtils.hasLength(host)) {
              throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
              }
              builder.host(host);
              String port = matcher.group(7);
              if (StringUtils.hasLength(port)) {
              builder.port(port);
              }
              builder.path(matcher.group(8));
              builder.query(matcher.group(10));
              return builder;
              }
              else {
              throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
              }
              }


              where you can notice that a pattern matcher is defined on an expected structure of the url and parts are parsed according to the matcher. If you see IP it means that the url specified in input (request.getRequestURL().toString()) contained the IP address as a host.



              This means that you should be looking for the guilty one above in the chain, starting by whoever calls this piece of code and following the links until you find the cause.






              share|improve this answer
























                0












                0








                0






                This is not a problem of the URIComponents, it parses what it gets in input. More specifically looking at the source of UriComponentsBuilder.fromHttpUrl you see:



                public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
                Assert.notNull(httpUrl, "HTTP URL must not be null");
                Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl);
                if (matcher.matches()) {
                UriComponentsBuilder builder = new UriComponentsBuilder();
                String scheme = matcher.group(1);
                builder.scheme(scheme != null ? scheme.toLowerCase() : null);
                builder.userInfo(matcher.group(4));
                String host = matcher.group(5);
                if (StringUtils.hasLength(scheme) && !StringUtils.hasLength(host)) {
                throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
                }
                builder.host(host);
                String port = matcher.group(7);
                if (StringUtils.hasLength(port)) {
                builder.port(port);
                }
                builder.path(matcher.group(8));
                builder.query(matcher.group(10));
                return builder;
                }
                else {
                throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
                }
                }


                where you can notice that a pattern matcher is defined on an expected structure of the url and parts are parsed according to the matcher. If you see IP it means that the url specified in input (request.getRequestURL().toString()) contained the IP address as a host.



                This means that you should be looking for the guilty one above in the chain, starting by whoever calls this piece of code and following the links until you find the cause.






                share|improve this answer












                This is not a problem of the URIComponents, it parses what it gets in input. More specifically looking at the source of UriComponentsBuilder.fromHttpUrl you see:



                public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
                Assert.notNull(httpUrl, "HTTP URL must not be null");
                Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl);
                if (matcher.matches()) {
                UriComponentsBuilder builder = new UriComponentsBuilder();
                String scheme = matcher.group(1);
                builder.scheme(scheme != null ? scheme.toLowerCase() : null);
                builder.userInfo(matcher.group(4));
                String host = matcher.group(5);
                if (StringUtils.hasLength(scheme) && !StringUtils.hasLength(host)) {
                throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
                }
                builder.host(host);
                String port = matcher.group(7);
                if (StringUtils.hasLength(port)) {
                builder.port(port);
                }
                builder.path(matcher.group(8));
                builder.query(matcher.group(10));
                return builder;
                }
                else {
                throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
                }
                }


                where you can notice that a pattern matcher is defined on an expected structure of the url and parts are parsed according to the matcher. If you see IP it means that the url specified in input (request.getRequestURL().toString()) contained the IP address as a host.



                This means that you should be looking for the guilty one above in the chain, starting by whoever calls this piece of code and following the links until you find the cause.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 11:31









                NiVeR

                6,80141930




                6,80141930






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410938%2furicomponents-returns-ip-instead-of-domain%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'