{"id":2612,"date":"2018-09-18T17:36:42","date_gmt":"2018-09-18T16:36:42","guid":{"rendered":"http:\/\/www.robertprice.co.uk\/robblog\/?p=2612"},"modified":"2018-09-18T17:36:42","modified_gmt":"2018-09-18T16:36:42","slug":"using-a-http-proxy-from-a-mendix-java-action","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/","title":{"rendered":"Using a http proxy from a Mendix Java action"},"content":{"rendered":"<p>As part of some work I have been undertaking to integrate the UK Government Notifications service into Mendix, I needed to be able to make API calls from behind a firewall using a proxy in a Java action.<\/p>\n<p>Due to the lower level Java actions in Mendix run at, proxy settings are not automatically applied, and must be added manually. I wanted to explain how to get the proxy settings from Mendix, and use them a Java action.<\/p>\n<p>I&#8217;ve previously explained how to <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/using-a-proxy-server-from-the-mendix-modeller\/\">add proxy settings to Mendix<\/a>, so I assume this step has been completed.<\/p>\n<p>In a Java action, we need to get these from the HttpConfiguration singleton.<\/p>\n<pre>\r\nimport com.mendix.http.HttpConfiguration;\r\nimport com.mendix.http.IHttpConfiguration;\r\nimport com.mendix.http.IProxyConfiguration;\r\n\r\nIHttpConfiguration httpconf = com.mendix.http.HttpConfiguration.getInstance();\r\nIProxyConfiguration proxyconf = httpconf.getProxyConfiguration().orElse(null);\r\n<\/pre>\n<p>We can now check if we have a proxy configuration set, if we don&#8217;t <code>proxyconf<\/code> will be <code>null<\/code>.<\/p>\n<p>The username and password for the proxy can be retrieved using the <code>getUser()<\/code> and <code>getPassword()<\/code> methods.<\/p>\n<pre>\r\nString username = proxyconf.getUser().orElse(null);\r\nString password = proxyconf.getPassword().orElse(null);\r\n<\/pre>\n<p>If they are present we can build a Java Authenticator object and set it as the default authenticator. <\/p>\n<pre>\r\nimport java.net.Authenticator;\r\nimport java.net.PasswordAuthentication;\r\n\r\nif (username != null && password != null) {\r\n    Authenticator authenticator = new Authenticator() {\r\n        public PasswordAuthentication getPasswordAuthentication() {\r\n           return (new PasswordAuthentication(username, password.toCharArray()));\r\n        }\r\n    };\r\n\r\n    Authenticator.setDefault(authenticator);\r\n}\r\n<\/pre>\n<p>Next we need to create the Proxy object. We need to get the host and port of our proxy server from Mendix using the <code>getHost()<\/code> and <code>getPort()<\/code> methods.<\/p>\n<pre>\r\nimport java.net.InetSocketAddress;\r\nimport java.net.Proxy;\r\n\r\nInetSocketAddress proxyLocation = new InetSocketAddress(proxyconf.getHost(), proxyconf.getPort());\r\nProxy proxy = new Proxy(Proxy.Type.HTTP, proxyLocation);\r\n<\/pre>\n<p>The <code>proxy<\/code> can be used for Java network actions. <\/p>\n<p>An example of using this would be the UK Government Notifications client. It has a second optional paramater in it&#8217;s constructor for a Proxy.<\/p>\n<pre>\r\nclient = new NotificationClient('APIKey', proxy);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>As part of some work I have been undertaking to integrate the UK Government Notifications service into Mendix, I needed to be able to make API calls from behind a firewall using a proxy in a Java action. Due to the lower level Java actions in Mendix run at, proxy settings are not automatically applied, &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using a http proxy from a Mendix Java action&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2611,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[30,98,52],"class_list":["post-2612","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev","tag-java","tag-mendix","tag-proxy"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using a http proxy from a Mendix Java action - Robert Price<\/title>\n<meta name=\"description\" content=\"An example of how to use an http proxy in a Mendix Java action.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using a http proxy from a Mendix Java action - Robert Price\" \/>\n<meta property=\"og:description\" content=\"An example of how to use an http proxy in a Mendix Java action.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-18T16:36:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/MendixModellerProxyFeatured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"768\" \/>\n\t<meta property=\"og:image:height\" content=\"384\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Using a http proxy from a Mendix Java action\",\"datePublished\":\"2018-09-18T16:36:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/\"},\"wordCount\":227,\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/MendixModellerProxyFeatured.png\",\"keywords\":[\"Java\",\"Mendix\",\"proxy\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/\",\"name\":\"Using a http proxy from a Mendix Java action - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/MendixModellerProxyFeatured.png\",\"datePublished\":\"2018-09-18T16:36:42+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"An example of how to use an http proxy in a Mendix Java action.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/MendixModellerProxyFeatured.png\",\"contentUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/MendixModellerProxyFeatured.png\",\"width\":768,\"height\":384},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-a-http-proxy-from-a-mendix-java-action\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using a http proxy from a Mendix Java action\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using a http proxy from a Mendix Java action - Robert Price","description":"An example of how to use an http proxy in a Mendix Java action.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/","og_locale":"en_GB","og_type":"article","og_title":"Using a http proxy from a Mendix Java action - Robert Price","og_description":"An example of how to use an http proxy in a Mendix Java action.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/","og_site_name":"Robert Price","article_published_time":"2018-09-18T16:36:42+00:00","og_image":[{"width":768,"height":384,"url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/MendixModellerProxyFeatured.png","type":"image\/png"}],"author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Using a http proxy from a Mendix Java action","datePublished":"2018-09-18T16:36:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/"},"wordCount":227,"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/MendixModellerProxyFeatured.png","keywords":["Java","Mendix","proxy"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/","name":"Using a http proxy from a Mendix Java action - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/#primaryimage"},"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/MendixModellerProxyFeatured.png","datePublished":"2018-09-18T16:36:42+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"An example of how to use an http proxy in a Mendix Java action.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/#primaryimage","url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/MendixModellerProxyFeatured.png","contentUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/MendixModellerProxyFeatured.png","width":768,"height":384},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-a-http-proxy-from-a-mendix-java-action\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Using a http proxy from a Mendix Java action"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=2612"}],"version-history":[{"count":5,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2612\/revisions"}],"predecessor-version":[{"id":2617,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2612\/revisions\/2617"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media\/2611"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=2612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=2612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=2612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}