javax.xml.parsers.DocumentBuilderFactory Scala Examples

The following examples show how to use javax.xml.parsers.DocumentBuilderFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: AhcWSClientSpec.scala    From play-ws   with Apache License 2.0 5 votes vote down vote up
package play.libs.ws.ahc

import akka.http.scaladsl.server.Route
import akka.stream.javadsl.Sink
import akka.util.ByteString
import org.specs2.concurrent.ExecutionEnv
import org.specs2.matcher.FutureMatchers
import org.specs2.mutable.Specification
import play.AkkaServerProvider
import play.libs.ws._

import scala.compat.java8.FutureConverters._
import scala.concurrent.Future
import scala.concurrent.duration._

class AhcWSClientSpec(implicit val executionEnv: ExecutionEnv)
    extends Specification
    with AkkaServerProvider
    with StandaloneWSClientSupport
    with FutureMatchers
    with XMLBodyWritables
    with XMLBodyReadables {

  override val routes: Route = {
    import akka.http.scaladsl.server.Directives._
    get {
      complete("<h1>Say hello to akka-http</h1>")
    } ~
      post {
        entity(as[String]) { echo =>
          complete(echo)
        }
      }
  }

  "play.libs.ws.ahc.StandaloneAhcWSClient" should {

    "get successfully" in withClient() { client =>
      def someOtherMethod(string: String) = {
        new InMemoryBodyWritable(akka.util.ByteString.fromString(string), "text/plain")
      }
      toScala(client.url(s"http://localhost:$testServerPort").post(someOtherMethod("hello world")))
        .map(response => response.getBody() must be_==("hello world"))
        .await(retries = 0, timeout = 5.seconds)
    }

    "source successfully" in withClient() { client =>
      val future = toScala(client.url(s"http://localhost:$testServerPort").stream())
      val result: Future[ByteString] = future.flatMap { response: StandaloneWSResponse =>
        toScala(response.getBodyAsSource.runWith(Sink.head[ByteString](), materializer))
      }
      val expected: ByteString = ByteString.fromString("<h1>Say hello to akka-http</h1>")
      result must be_==(expected).await(retries = 0, timeout = 5.seconds)
    }

    "round trip XML successfully" in withClient() { client =>
      val document = XML.fromString("""<?xml version="1.0" encoding='UTF-8'?>
                                      |<note>
                                      |  <from>hello</from>
                                      |  <to>world</to>
                                      |</note>""".stripMargin)
      document.normalizeDocument()

      toScala {
        client.url(s"http://localhost:$testServerPort").post(body(document))
      }.map { response =>
          import javax.xml.parsers.DocumentBuilderFactory
          val dbf = DocumentBuilderFactory.newInstance
          dbf.setNamespaceAware(true)
          dbf.setCoalescing(true)
          dbf.setIgnoringElementContentWhitespace(true)
          dbf.setIgnoringComments(true)
          dbf.newDocumentBuilder

          val responseXml = response.getBody(xml())
          responseXml.normalizeDocument()

          (responseXml.isEqualNode(document) must beTrue).and {
            response.getUri must beEqualTo(new java.net.URI(s"http://localhost:$testServerPort"))
          }
        }
        .await(retries = 0, timeout = 5.seconds)
    }
  }
} 
Example 2
Source File: AdWordsClient.scala    From spark-google-adwords   with Apache License 2.0 5 votes vote down vote up
package com.crealytics.google.adwords

import java.util.zip.GZIPInputStream
import javax.xml.parsers.DocumentBuilderFactory

import com.google.api.ads.adwords.axis.factory.AdWordsServices
import com.google.api.ads.adwords.axis.v201806.cm.{ReportDefinitionField, ReportDefinitionServiceInterface}
import com.google.api.ads.adwords.axis.v201806.cm.{
  ReportDefinitionField,
  ReportDefinitionReportType,
  ReportDefinitionServiceInterface
}
import com.google.api.ads.adwords.lib.client.AdWordsSession
import com.google.api.ads.adwords.lib.jaxb.v201806.{DownloadFormat}
import com.google.api.ads.adwords.lib.jaxb.v201806.DownloadFormat
import com.google.api.ads.adwords.lib.utils.v201806.ReportDownloader
import com.google.api.ads.adwords.lib.utils.v201806.ReportDownloader
import com.google.api.ads.common.lib.auth.OfflineCredentials
import com.google.api.client.auth.oauth2.Credential

class AdWordsClient(credential: Credential, developerToken: String, userAgent: String, clientCustomerId: String) {

  // The Adwords API Session
  private lazy val session =
    new AdWordsSession.Builder()
      .withDeveloperToken(developerToken)
      .withUserAgent(userAgent)
      .withOAuth2Credential(credential)
      .withClientCustomerId(clientCustomerId)
      .build

  // Factory for all AdWords Services
  private lazy val services = new AdWordsServices()

  // Report Definition Service: Provides Metadata about Reports (Column Names, Data Types...)
  private lazy val reportDefinitionService =
    services.get(session, classOf[ReportDefinitionServiceInterface])

  // Returns Field Descriptors for all possible Fields of this Report
  def getFieldsForReportType(report: String): Array[ReportDefinitionField] =
    reportDefinitionService.getReportFields(ReportDefinitionReportType.fromValue(report))

  // Executes the Query, downloads the Report and parses the XML into a Sequence of Rows
  def downloadReport(query: String): Seq[Map[String, String]] = {
    // download the report
    val reportResponse = new ReportDownloader(session)
      .downloadReport(query, DownloadFormat.GZIPPED_XML)
    val inputStream = reportResponse.getInputStream
    val zipStream = new GZIPInputStream(inputStream)
    val xmlDocument =
      DocumentBuilderFactory.newInstance.newDocumentBuilder().parse(zipStream)
    val nodes = xmlDocument.getElementsByTagName("row")
    // Loop over the nodes
    (0 until nodes.getLength)
      .map(i => {
        val item = nodes.item(i)
        // All data is stored in attributes, so put the attributes into a map
        val attrs = item.getAttributes
        (0 until attrs.getLength)
          .map(j => (attrs.item(j).getNodeName, attrs.item(j).getNodeValue))
          .toMap
      })
      .toSeq
  }

}