1 / 53

Bare-knuckle web development

Bare-knuckle web development. XP Days Ukraine Johannes Brodwall, Chief scientist Exilesoft Global. Bare-knuckle philosophy Demonstration of bare-knuckle web in Java Further directions. Part I:. The bare-knuckle philosophy. High impact with low ceremony. Framework light Test-driven

harley
Download Presentation

Bare-knuckle web development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bare-knuckle web development XP Days Ukraine Johannes Brodwall, Chief scientist Exilesoft Global

  2. Bare-knuckle philosophy Demonstration of bare-knuckle web in Java Further directions

  3. Part I:

  4. The bare-knuckle philosophy

  5. High impact with low ceremony

  6. Framework light Test-driven No calculators

  7. Light on framework

  8. Frameworks solve 80% of the job…

  9. … and makes the rest 10 times as hard

  10. “Why did Hibernate suddenly slow down?”

  11. “How do I implement a custom SOAP header with JAX-WS?”

  12. “How to do X with Spring”

  13. @AutoWire + package scan with 100s of beans

  14. Test-driven

  15. No more architecture than what’s needed

  16. Fast feedback cycle – also in the future

  17. Don’t use a calculator…

  18. Part II:

  19. Demo: Phonebook web app

  20. Test driving

  21. WebDriver browser = createWebDriver(); browser.get(url); browser.findElement(By.linkText("Add contact")).click(); browser.findEleme(By.name("fullName")).sendKeys("Vader"); browser.findEleme(By.name("phoneNumber")).sendKeys("27"); browser.findEleme(By.name("saveContact")).click(); browser.findElement(By.linkText("Find contact")).click(); browser.findElem(By.name("nameQuery")).sendKeys("vader"); browser.findElement(By.name("nameQuery")).submit(); assertThat(browser.findElem(By.id("contacts")).getText()) .contains("555-33274-7827");

  22. Server server = new Server(0); server.setHandler(newWebAppContext("src/main/webapp", "/contacts")); server.start(); int port = server.getConnectors()[0].getLocalPort(); String url = "http://localhost:" + port + "/contacts";

  23. <web-app version="2.5“> <servlet> <servlet-name>contactServlet</servlet-name> <servlet-class>com.exilesoft.bareknuckleweb.ContactServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>contactServlet</servlet-name> <url-pattern>contact/*</url-pattern> </servlet-mapping> </web-app>

  24. publicclassContactServletextendsHttpServlet{ }

  25. @Test publicvoidshouldShowAddForm() throws Exception { ContactServlet servlet = newContactServlet(); HttpServletRequestreq = mock(HttpServletRequest.class); HttpServletResponseresp = mock(HttpServletResponse.class); StringWriter html = newStringWriter(); when(resp.getWriter()).thenReturn(newPrintWriter(html)); when(req.getPathInfo()).thenReturn("/create.html"); servlet.doGet(req, resp); verify(resp).setContentType("text/html"); assertThat(html.toString()) .contains("<form method='post'") .contains("<input type='text' name='fullName'") .contains("<input type='text' name='phoneNumber'") .contains("<input type='submit' name='createContact'"); }

  26. Refactoring

  27. voidshowFindPage(String q, PrintWriter writer) { Document doc = Xml.read("contact/index.html"); doc.selectFirst("[name=nameQuery]").val(nameQuery); Element contactsEl = doc.select("#contacts"); Element template = contactsEl.select(".contact"); contactsEl.clear(); for (Contact contact : contactRepository.find(q)) { contactsEl.add(template.copy().text(contact.print())); } doc.write(writer); }

  28. .NET

  29. [TestMethod] publicvoidShouldFindSavedContacts() { var server = newWebServer(); server.Start("http://localhost:12380/"); varurl = "http://localhost:12380"; var browser = newSimpleBrowser.WebDriver.SimpleBrowserDriver(); browser.Url = url + "/contacts"; browser.FindElement(By.LinkText("Add contact")).Click(); browser.FindElement(By.Name("fullName")).SendKeys("Darth Vader"); browser.FindElement(By.Name("phoneNumber")).SendKeys("555-33274-7827"); browser.FindElement(By.Name("saveContact")).Click(); browser.FindElement(By.LinkText("Find contact")).Click(); browser.FindElement(By.Name("nameQuery")).SendKeys("vader"); browser.FindElement(By.Name("nameQuery")).Submit(); browser.FindElement(By.Id("contacts")).Text.Should() .Contain("555-33274-7827"); }

  30. publicclassWebServer { publicvoid Start(stringbaseAddress) { varconfig = newHttpSelfHostConfiguration(baseAddress); config.Routes.MapHttpRoute("web Default", "{controller}/{id}",new{ id = RouteParameter.Optional }); using (var server = newHttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("Press Enter to quit."); Console.ReadLine(); } } }

  31. Part III:

  32. Further directions

  33. Norwegian agricultural authority

  34. Java web application with an MVC architecture

  35. Controllers: Create a view Retrieve model from repo Set model on view Render view

  36. View example:

  37. @Override publicvoidrender(HttpServletResponseresp) throwsIOException { Match document = $("html", head(), $("img").attr("src", "/sms-varsel/Sparebank1.jpg"), $("h1", "Internet bank simulator"), $("form").attr("method", "post").append( hiddenField(this.bankNum, "bankNum"), hiddenField(this.customerId, "customerId"), $("h2", "Set Mobile Phone Number"), phoneNumberField(this.phoneNumber), $("h2", "Account numbers"), accountNumbersField(this.accountNumbers), $("h2", "Payment account"), paymentAccountField(this.defaultAccount), $("h2", "Save changes"), $("div", $("input").attr("type", "submit").attr("value", "Store")).attr("name", "update"))); resp.setContentType("text/html"); resp.setCharacterEncoding("UTF-8"); resp.getWriter().write(document.toString()); }

  38. Match document = $("html", head(), $("img").attr("src", "/logo.jpg"), $("h1", “Page name"), $("form").attr("method", "post").append( hiddenField(this.bankNum, "bankNum"), hiddenField(this.customerId, "customerId"), $("h2", "Save changes"), $("div", $("input").attr("type", "submit") .attr("value", "Store")) .attr("name", "update")));

  39. Norwegian Power Transmission System Operator

  40. Universal repository Universal service Commands and Queries One domain model

  41. No Spring – 100 KLOC

  42. Single-jar deployment Includes scripts Includes Jetty

  43. publicclassStatnettWebServer { privatefinalorg.eclipse.jetty.server.Serverserver; publicContactWebServer(int port) { server = new Server(port); server.setHandler(newWebAppContext(“…", "/statnett")); } void start() throws Exception { server.start(); } String getUrl() { int port = server.getConnectors()[0].getLocalPort(); return"http://localhost:" + port + "/contacts"; } publicstaticvoid main(String[] args) throws Exception { StatnettWebServerserver = newStatnettWebServer(10080); server.start(); System.out.println(server.getUrl()); } }

  44. SpareBank1

  45. 10 web service clients

  46. HttpURLConnection JOOX

  47. @Override public String getCountryByIp(String ipAddress) throws Exception { Document soapRequest = soapElement("S:Envelope", $("S:Body", wsxElement("wsx:GetGeoIP", $("wsx:IPAddress", ipAddress)))); Document soapResponseendpoint.postRequest(getSOAPAction(), soapRequest); return$(soapResponse).xpath("/Envelope/Body/*") .xpath("GetGeoIPResult/CountryName").text(); }

  48. public Document postRequest(String soapAction, Document soapRequest) { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.addRequestProperty("SOAPAction", soapAction); connection.addRequestProperty("Content-Type", "text/xml"); $(soapRequest).write(connection.getOutputStream()); intresponseCode = connection.getResponseCode(); if (isErrorResponse(responseCode)) { String response = toString(connection.getErrorStream()); String responseContentType = connection.getContentType(); if (responseContentType.startsWith("text/xml")) { return response; } thrownewServiceCommunicationException( "On POST to " + url + ": " + responseCode + " " + connection.getResponseMessage() + ": " + response); } return$(connection.getInputStream()).document(); d}

  49. Conclusion:

  50. YAGNI

More Related