Permalink
Browse files

Reformatted code: tabs -> 2 spaces, line length 80 -> 120

  • Loading branch information...
1 parent 0515ba0 commit 8650935ac5a79e5579a87c3611a34145f170c5bb @Villane committed Oct 5, 2011
View
90 src/main/java/org/villane/shttpc/BetterXPath.scala
@@ -1,45 +1,45 @@
-package org.villane.shttpc
-
-import xml._
-
-object BetterXPath {
- implicit def betterXPath(node: NodeSeq) = new BetterXPath(node)
-}
-
-/**
- * Supports a subset of XPath expressions in the form of
- *
- * /tag1/tag2[@attr='stringLiteral']/tag2
- * /tag1/tag2[tag4=@attr2]/tag2
- */
-class BetterXPath(node: NodeSeq) {
- import BetterXPath._
-
- def /(path: String): NodeSeq = {
- if (path.contains("/")) {
- val segs = path.split("/")
- var n = node
- for (seg <- segs) n /= seg
- n
- } else if (path.contains("[")) {
- val segs = path.split("[\\[\\]]")
- val ns = node \ segs(0)
- val Array(left,right) = segs(1).split("=")
- ns filter { n =>
- evaluatePredicate(n, left) == evaluatePredicate(n, right)
- }
- } else {
- node \ path
- }
- }
-
- private def evaluatePredicate(n: NodeSeq, expr: String) = {
- if (expr.startsWith("'") && expr.endsWith("'")) {
- // string literal
- expr.substring(1, expr.length - 1)
- } else {
- // usual attribute or element name
- n \ expr
- }
- }
-}
+package org.villane.shttpc
+
+import xml._
+
+object BetterXPath {
+ implicit def betterXPath(node: NodeSeq) = new BetterXPath(node)
+}
+
+/**
+ * Supports a subset of XPath expressions in the form of
+ *
+ * /tag1/tag2[@attr='stringLiteral']/tag2
+ * /tag1/tag2[tag4=@attr2]/tag2
+ */
+class BetterXPath(node: NodeSeq) {
+ import BetterXPath._
+
+ def /(path: String): NodeSeq = {
+ if (path.contains("/")) {
+ val segs = path.split("/")
+ var n = node
+ for (seg <- segs) n /= seg
+ n
+ } else if (path.contains("[")) {
+ val segs = path.split("[\\[\\]]")
+ val ns = node \ segs(0)
+ val Array(left, right) = segs(1).split("=")
+ ns filter { n =>
+ evaluatePredicate(n, left) == evaluatePredicate(n, right)
+ }
+ } else {
+ node \ path
+ }
+ }
+
+ private def evaluatePredicate(n: NodeSeq, expr: String) = {
+ if (expr.startsWith("'") && expr.endsWith("'")) {
+ // string literal
+ expr.substring(1, expr.length - 1)
+ } else {
+ // usual attribute or element name
+ n \ expr
+ }
+ }
+}
View
125 src/main/java/org/villane/shttpc/Http.java
@@ -24,82 +24,75 @@
* @author erkki.lindpere
*/
public class Http {
- public static String DefaultPostEncoding = "UTF-8";
- public static String DefaultURIEncoding = "UTF-8";
+ public static String DefaultPostEncoding = "UTF-8";
+ public static String DefaultURIEncoding = "UTF-8";
- public final HttpClient client;
+ public final HttpClient client;
- public Http() {
- this.client = new DefaultHttpClient();
- }
+ public Http() {
+ this.client = new DefaultHttpClient();
+ }
- public Http(HttpClient client) {
- this.client = client;
- }
+ public Http(HttpClient client) {
+ this.client = client;
+ }
- public static Http newTrustingInstace() {
- return new Http(new TrustingHttpClient());
- }
+ public static Http newTrustingInstace() {
+ return new Http(new TrustingHttpClient());
+ }
- public SimpleHttpResponse get(String uri) throws ClientProtocolException,
- IOException {
- HttpGet get = new HttpGet(uri);
- return new SimpleHttpResponse(client.execute(get));
- }
+ public SimpleHttpResponse get(String uri) throws ClientProtocolException, IOException {
+ HttpGet get = new HttpGet(uri);
+ return new SimpleHttpResponse(client.execute(get));
+ }
- public SimpleHttpResponse get(String uri, Object... params)
- throws ClientProtocolException, IOException {
- return get(formatURI(uri, params));
- }
+ public SimpleHttpResponse get(String uri, Object... params) throws ClientProtocolException, IOException {
+ return get(formatURI(uri, params));
+ }
- public SimpleHttpResponse post(String uri, Object[] uriParams,
- Map<String, String> postParams) throws ClientProtocolException,
- IOException {
- return post(formatURI(uri, uriParams), postParams);
- }
+ public SimpleHttpResponse post(String uri, Object[] uriParams, Map<String, String> postParams)
+ throws ClientProtocolException, IOException {
+ return post(formatURI(uri, uriParams), postParams);
+ }
- public SimpleHttpResponse post(String uri, Map<String, String> postParams)
- throws ClientProtocolException, IOException {
- List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
- for (Map.Entry<String, String> param : postParams.entrySet()) {
- paramsList.add(new BasicNameValuePair(param.getKey(), param
- .getValue()));
- }
- HttpPost post = new HttpPost(uri);
- UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramsList,
- DefaultPostEncoding);
- post.setEntity(entity);
- return new SimpleHttpResponse(client.execute(post));
- }
+ public SimpleHttpResponse post(String uri, Map<String, String> postParams) throws ClientProtocolException,
+ IOException {
+ List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
+ for (Map.Entry<String, String> param : postParams.entrySet()) {
+ paramsList.add(new BasicNameValuePair(param.getKey(), param.getValue()));
+ }
+ HttpPost post = new HttpPost(uri);
+ UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramsList, DefaultPostEncoding);
+ post.setEntity(entity);
+ return new SimpleHttpResponse(client.execute(post));
+ }
- public SimpleHttpResponse delete(String uri)
- throws ClientProtocolException, IOException {
- HttpDelete del = new HttpDelete(uri);
- return new SimpleHttpResponse(client.execute(del));
- }
+ public SimpleHttpResponse delete(String uri) throws ClientProtocolException, IOException {
+ HttpDelete del = new HttpDelete(uri);
+ return new SimpleHttpResponse(client.execute(del));
+ }
- public SimpleHttpResponse delete(String uri, Object... params)
- throws ClientProtocolException, IOException {
- return delete(formatURI(uri, params));
- }
+ public SimpleHttpResponse delete(String uri, Object... params) throws ClientProtocolException, IOException {
+ return delete(formatURI(uri, params));
+ }
- protected static String formatURI(String uri, Object... params) {
- if (params.length > 0) {
- String[] encodedParams = new String[params.length];
- for (int i = 0; i < params.length; i++) {
- encodedParams[i] = uriEncode(params[i].toString());
- }
- return MessageFormatter.arrayFormat(uri, encodedParams).getMessage();
- }
- return uri;
- }
+ protected static String formatURI(String uri, Object... params) {
+ if (params.length > 0) {
+ String[] encodedParams = new String[params.length];
+ for (int i = 0; i < params.length; i++) {
+ encodedParams[i] = uriEncode(params[i].toString());
+ }
+ return MessageFormatter.arrayFormat(uri, encodedParams).getMessage();
+ }
+ return uri;
+ }
- @SuppressWarnings("deprecation")
- protected static String uriEncode(String value) {
- try {
- return URLEncoder.encode(value, DefaultURIEncoding);
- } catch (UnsupportedEncodingException e) {
- return URLEncoder.encode(value);
- }
- }
+ @SuppressWarnings("deprecation")
+ protected static String uriEncode(String value) {
+ try {
+ return URLEncoder.encode(value, DefaultURIEncoding);
+ } catch (UnsupportedEncodingException e) {
+ return URLEncoder.encode(value);
+ }
+ }
}
View
100 src/main/java/org/villane/shttpc/IterableNodeList.java
@@ -1,50 +1,50 @@
-package org.villane.shttpc;
-
-import java.util.Iterator;
-
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-public class IterableNodeList implements Iterable<Node>, NodeList {
-
- public final NodeList nodeList;
-
- public IterableNodeList(NodeList nodeList) {
- this.nodeList = nodeList;
- }
-
- @Override
- public Iterator<Node> iterator() {
- return new NodeListIterator();
- }
-
- public int getLength() {
- return nodeList.getLength();
- }
-
- public Node item(int index) {
- return nodeList.item(index);
- }
-
- public class NodeListIterator implements Iterator<Node> {
-
- private int idx = 0;
-
- @Override
- public boolean hasNext() {
- return idx < nodeList.getLength();
- }
-
- @Override
- public Node next() {
- idx += 1;
- return nodeList.item(idx - 1);
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- }
-}
+package org.villane.shttpc;
+
+import java.util.Iterator;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+public class IterableNodeList implements Iterable<Node>, NodeList {
+
+ public final NodeList nodeList;
+
+ public IterableNodeList(NodeList nodeList) {
+ this.nodeList = nodeList;
+ }
+
+ @Override
+ public Iterator<Node> iterator() {
+ return new NodeListIterator();
+ }
+
+ public int getLength() {
+ return nodeList.getLength();
+ }
+
+ public Node item(int index) {
+ return nodeList.item(index);
+ }
+
+ public class NodeListIterator implements Iterator<Node> {
+
+ private int idx = 0;
+
+ @Override
+ public boolean hasNext() {
+ return idx < nodeList.getLength();
+ }
+
+ @Override
+ public Node next() {
+ idx += 1;
+ return nodeList.item(idx - 1);
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ }
+}
View
66 src/main/java/org/villane/shttpc/NekoHTML.scala
@@ -1,33 +1,33 @@
-package org.villane.shttpc
-
-import xml._
-import xml.factory._
-import org.xml.sax.InputSource
-
-object NekoHTML extends XMLLoader[Elem] {
-
- def nekoParser = {
- val parser = new org.cyberneko.html.parsers.SAXParser
- parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower")
- parser
- }
-
- /**
- * Note: Ignores the second argument.
- */
- override def loadXML(source: InputSource, parser: javax.xml.parsers.SAXParser): Elem = {
- val newAdapter = adapter
- val p = nekoParser
-
- newAdapter.scopeStack push TopScope
- p.setContentHandler(newAdapter)
- p.setEntityResolver(newAdapter)
- p.setErrorHandler(newAdapter)
- p.setDTDHandler(newAdapter)
- p.parse(source)
- newAdapter.scopeStack.pop
-
- newAdapter.rootElem.asInstanceOf[Elem]
- }
-
-}
+package org.villane.shttpc
+
+import xml._
+import xml.factory._
+import org.xml.sax.InputSource
+
+object NekoHTML extends XMLLoader[Elem] {
+
+ def nekoParser = {
+ val parser = new org.cyberneko.html.parsers.SAXParser
+ parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower")
+ parser
+ }
+
+ /**
+ * Note: Ignores the second argument.
+ */
+ override def loadXML(source: InputSource, parser: javax.xml.parsers.SAXParser): Elem = {
+ val newAdapter = adapter
+ val p = nekoParser
+
+ newAdapter.scopeStack push TopScope
+ p.setContentHandler(newAdapter)
+ p.setEntityResolver(newAdapter)
+ p.setErrorHandler(newAdapter)
+ p.setDTDHandler(newAdapter)
+ p.parse(source)
+ newAdapter.scopeStack.pop
+
+ newAdapter.rootElem.asInstanceOf[Elem]
+ }
+
+}
View
72 src/main/java/org/villane/shttpc/RichHttpResponse.scala
@@ -1,36 +1,36 @@
-package org.villane.shttpc
-
-import xml._
-import xml.factory.XMLLoader
-import net.liftweb.json._
-
-/**
- * Adds convenience methods to SimpleHttpResponse. Should be used only via implicit conversions
- */
-class RichHttpResponse(val response: SimpleHttpResponse) {
- def asXml: Elem = asXml(XML)
- def asXml[T <: Node](loader: XMLLoader[T]) = {
- val xml = loader.load(response.asInputStream)
- response.consume
- xml
- }
-
- def asHtml: Elem = asHtml(DefaultHtmlLoader)
- def asHtml[T <: Node](loader: XMLLoader[T]) = {
- val xml = loader.load(response.asInputStream)
- response.consume
- xml
- }
-
- def asJson: JsonAST.JValue = JsonParser.parse(response.asText)
-
- def header(name: String) = response.response.getFirstHeader(name) match {
- case null => None
- case h => Some(h)
- }
-
- def getRedirect = header("Location") match {
- case Some(loc) => Some((response.statusCode, loc.getValue))
- case None => None
- }
-}
+package org.villane.shttpc
+
+import xml._
+import xml.factory.XMLLoader
+import net.liftweb.json._
+
+/**
+ * Adds convenience methods to SimpleHttpResponse. Should be used only via implicit conversions
+ */
+class RichHttpResponse(val response: SimpleHttpResponse) {
+ def asXml: Elem = asXml(XML)
+ def asXml[T <: Node](loader: XMLLoader[T]) = {
+ val xml = loader.load(response.asInputStream)
+ response.consume
+ xml
+ }
+
+ def asHtml: Elem = asHtml(DefaultHtmlLoader)
+ def asHtml[T <: Node](loader: XMLLoader[T]) = {
+ val xml = loader.load(response.asInputStream)
+ response.consume
+ xml
+ }
+
+ def asJson: JsonAST.JValue = JsonParser.parse(response.asText)
+
+ def header(name: String) = response.response.getFirstHeader(name) match {
+ case null => None
+ case h => Some(h)
+ }
+
+ def getRedirect = header("Location") match {
+ case Some(loc) => Some((response.statusCode, loc.getValue))
+ case None => None
+ }
+}
View
178 src/main/java/org/villane/shttpc/SimpleHttpResponse.java
@@ -21,94 +21,94 @@
import org.xml.sax.SAXException;
public class SimpleHttpResponse {
- public static final String DefaultEncoding = "UTF-8";
- public final HttpResponse response;
- private MappingJsonFactory jsonFactory = null;
-
- public SimpleHttpResponse(HttpResponse response) {
- this.response = response;
- }
-
- public Header[] headers() {
- return response.getAllHeaders();
- }
-
- public Header[] headers(String name) {
- return response.getHeaders(name);
- }
-
- public int statusCode() {
- return response.getStatusLine().getStatusCode();
- }
-
- public String reasonPhrase() {
- return response.getStatusLine().getReasonPhrase();
- }
-
- public InputStream asInputStream() throws IOException {
- return response.getEntity().getContent();
- }
-
- public byte[] asBytes() throws IOException {
- ByteArrayOutputStream buf = new ByteArrayOutputStream();
- response.getEntity().writeTo(buf);
- EntityUtils.consume(response.getEntity());
- return buf.toByteArray();
- }
-
- public String asText(String encoding) throws IOException {
- ByteArrayOutputStream buf = new ByteArrayOutputStream();
- response.getEntity().writeTo(buf);
- EntityUtils.consume(response.getEntity());
- return buf.toString(encoding);
- }
-
- public String asText() throws IOException {
- String enc = declaredCharSet();
- return enc != null ? asText(enc) : asText(DefaultEncoding);
- }
-
- public Document asDomXml() throws IOException, ParserConfigurationException, SAXException {
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.parse(response.getEntity().getContent());
- EntityUtils.consume(response.getEntity());
- return doc;
- }
-
- public Document asDomHtml() throws IOException, ParserConfigurationException, SAXException {
- DOMParser parser = new DOMParser();
- parser.parse(new InputSource(response.getEntity().getContent()));
- EntityUtils.consume(response.getEntity());
- return parser.getDocument();
- }
-
- public JsonNode asJackson() throws JsonParseException, IOException {
- if (jsonFactory == null) {
- jsonFactory = new MappingJsonFactory();
- }
- JsonParser jsonParser = jsonFactory.createJsonParser(asInputStream());
- JsonNode value = jsonParser.readValueAsTree();
- EntityUtils.consume(response.getEntity());
- return value;
- }
-
- public <T> T mapWithJackson(Class<T> t) throws JsonParseException, IOException {
- if (jsonFactory == null) {
- jsonFactory = new MappingJsonFactory();
- }
- JsonParser jsonParser = jsonFactory.createJsonParser(asInputStream());
- T value = jsonParser.readValueAs(t);
- EntityUtils.consume(response.getEntity());
- return value;
- }
-
- public String declaredCharSet() {
- return EntityUtils.getContentCharSet(response.getEntity());
- }
-
- public void consume() throws IOException {
- EntityUtils.consume(response.getEntity());
- }
+ public static final String DefaultEncoding = "UTF-8";
+ public final HttpResponse response;
+ private MappingJsonFactory jsonFactory = null;
+
+ public SimpleHttpResponse(HttpResponse response) {
+ this.response = response;
+ }
+
+ public Header[] headers() {
+ return response.getAllHeaders();
+ }
+
+ public Header[] headers(String name) {
+ return response.getHeaders(name);
+ }
+
+ public int statusCode() {
+ return response.getStatusLine().getStatusCode();
+ }
+
+ public String reasonPhrase() {
+ return response.getStatusLine().getReasonPhrase();
+ }
+
+ public InputStream asInputStream() throws IOException {
+ return response.getEntity().getContent();
+ }
+
+ public byte[] asBytes() throws IOException {
+ ByteArrayOutputStream buf = new ByteArrayOutputStream();
+ response.getEntity().writeTo(buf);
+ EntityUtils.consume(response.getEntity());
+ return buf.toByteArray();
+ }
+
+ public String asText(String encoding) throws IOException {
+ ByteArrayOutputStream buf = new ByteArrayOutputStream();
+ response.getEntity().writeTo(buf);
+ EntityUtils.consume(response.getEntity());
+ return buf.toString(encoding);
+ }
+
+ public String asText() throws IOException {
+ String enc = declaredCharSet();
+ return enc != null ? asText(enc) : asText(DefaultEncoding);
+ }
+
+ public Document asDomXml() throws IOException, ParserConfigurationException, SAXException {
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ Document doc = db.parse(response.getEntity().getContent());
+ EntityUtils.consume(response.getEntity());
+ return doc;
+ }
+
+ public Document asDomHtml() throws IOException, ParserConfigurationException, SAXException {
+ DOMParser parser = new DOMParser();
+ parser.parse(new InputSource(response.getEntity().getContent()));
+ EntityUtils.consume(response.getEntity());
+ return parser.getDocument();
+ }
+
+ public JsonNode asJackson() throws JsonParseException, IOException {
+ if (jsonFactory == null) {
+ jsonFactory = new MappingJsonFactory();
+ }
+ JsonParser jsonParser = jsonFactory.createJsonParser(asInputStream());
+ JsonNode value = jsonParser.readValueAsTree();
+ EntityUtils.consume(response.getEntity());
+ return value;
+ }
+
+ public <T> T mapWithJackson(Class<T> t) throws JsonParseException, IOException {
+ if (jsonFactory == null) {
+ jsonFactory = new MappingJsonFactory();
+ }
+ JsonParser jsonParser = jsonFactory.createJsonParser(asInputStream());
+ T value = jsonParser.readValueAs(t);
+ EntityUtils.consume(response.getEntity());
+ return value;
+ }
+
+ public String declaredCharSet() {
+ return EntityUtils.getContentCharSet(response.getEntity());
+ }
+
+ public void consume() throws IOException {
+ EntityUtils.consume(response.getEntity());
+ }
}
View
74 src/main/java/org/villane/shttpc/SimpleXPath.java
@@ -1,40 +1,34 @@
-package org.villane.shttpc;
-
-import javax.xml.xpath.XPath;
-import javax.xml.xpath.XPathConstants;
-import javax.xml.xpath.XPathExpressionException;
-import javax.xml.xpath.XPathFactory;
-
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-public class SimpleXPath {
- private static final XPathFactory factory = XPathFactory.newInstance();
- private static final XPath xp = factory.newXPath();
-
- public static IterableNodeList evalNodeList(Node node, String xPath)
- throws XPathExpressionException {
- return new IterableNodeList((NodeList) xp.evaluate(xPath, node,
- XPathConstants.NODESET));
- }
-
- public static Node evalNode(Node node, String xPath)
- throws XPathExpressionException {
- return (Node) xp.evaluate(xPath, node, XPathConstants.NODE);
- }
-
- public static String evalString(Node node, String xPath)
- throws XPathExpressionException {
- return (String) xp.evaluate(xPath, node, XPathConstants.STRING);
- }
-
- public static Double evalNumber(Node node, String xPath)
- throws XPathExpressionException {
- return (Double) xp.evaluate(xPath, node, XPathConstants.NUMBER);
- }
-
- public static Boolean evalBoolean(Node node, String xPath)
- throws XPathExpressionException {
- return (Boolean) xp.evaluate(xPath, node, XPathConstants.BOOLEAN);
- }
-}
+package org.villane.shttpc;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+public class SimpleXPath {
+ private static final XPathFactory factory = XPathFactory.newInstance();
+ private static final XPath xp = factory.newXPath();
+
+ public static IterableNodeList evalNodeList(Node node, String xPath) throws XPathExpressionException {
+ return new IterableNodeList((NodeList) xp.evaluate(xPath, node, XPathConstants.NODESET));
+ }
+
+ public static Node evalNode(Node node, String xPath) throws XPathExpressionException {
+ return (Node) xp.evaluate(xPath, node, XPathConstants.NODE);
+ }
+
+ public static String evalString(Node node, String xPath) throws XPathExpressionException {
+ return (String) xp.evaluate(xPath, node, XPathConstants.STRING);
+ }
+
+ public static Double evalNumber(Node node, String xPath) throws XPathExpressionException {
+ return (Double) xp.evaluate(xPath, node, XPathConstants.NUMBER);
+ }
+
+ public static Boolean evalBoolean(Node node, String xPath) throws XPathExpressionException {
+ return (Boolean) xp.evaluate(xPath, node, XPathConstants.BOOLEAN);
+ }
+}
View
22 src/main/java/org/villane/shttpc/TagSoup.scala
@@ -1,11 +1,11 @@
-package org.villane.shttpc
-
-import xml._
-import xml.factory._
-import org.ccil.cowan.tagsoup.jaxp.SAXFactoryImpl
-
-object TagSoup extends XMLLoader[Elem] {
- private val saxFactory = new SAXFactoryImpl
-
- override def parser = saxFactory.newSAXParser
-}
+package org.villane.shttpc
+
+import xml._
+import xml.factory._
+import org.ccil.cowan.tagsoup.jaxp.SAXFactoryImpl
+
+object TagSoup extends XMLLoader[Elem] {
+ private val saxFactory = new SAXFactoryImpl
+
+ override def parser = saxFactory.newSAXParser
+}
View
80 src/main/java/org/villane/shttpc/TrustingHttpClient.java
@@ -22,52 +22,52 @@
*/
public class TrustingHttpClient extends DefaultHttpClient {
- public static final class BlindTrustStrategy implements TrustStrategy {
+ public static final class BlindTrustStrategy implements TrustStrategy {
- @Override
- public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
- return true;
- }
+ @Override
+ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
+ return true;
+ }
- }
+ }
- @Override
- protected ClientConnectionManager createClientConnectionManager() {
- SchemeRegistry registry = new SchemeRegistry();
- try {
- SSLSocketFactory naiveSocketFactory = new SSLSocketFactory(new BlindTrustStrategy(),
- SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
- registry.register(new Scheme("https", 443, naiveSocketFactory));
- } catch (Exception e) {
- throw new RuntimeException("Unable to create naive SSLSocketFactory", e);
- }
+ @Override
+ protected ClientConnectionManager createClientConnectionManager() {
+ SchemeRegistry registry = new SchemeRegistry();
+ try {
+ SSLSocketFactory naiveSocketFactory = new SSLSocketFactory(new BlindTrustStrategy(),
+ SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
+ registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
+ registry.register(new Scheme("https", 443, naiveSocketFactory));
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to create naive SSLSocketFactory", e);
+ }
- ClientConnectionManager connManager = null;
- HttpParams params = getParams();
+ ClientConnectionManager connManager = null;
+ HttpParams params = getParams();
- ClientConnectionManagerFactory factory = null;
+ ClientConnectionManagerFactory factory = null;
- String className = (String) params.getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME);
- if (className != null) {
- try {
- Class<?> clazz = Class.forName(className);
- factory = (ClientConnectionManagerFactory) clazz.newInstance();
- } catch (ClassNotFoundException ex) {
- throw new IllegalStateException("Invalid class name: " + className);
- } catch (IllegalAccessException ex) {
- throw new IllegalAccessError(ex.getMessage());
- } catch (InstantiationException ex) {
- throw new InstantiationError(ex.getMessage());
- }
- }
- if (factory != null) {
- connManager = factory.newInstance(params, registry);
- } else {
- connManager = new SingleClientConnManager(registry);
- }
+ String className = (String) params.getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME);
+ if (className != null) {
+ try {
+ Class<?> clazz = Class.forName(className);
+ factory = (ClientConnectionManagerFactory) clazz.newInstance();
+ } catch (ClassNotFoundException ex) {
+ throw new IllegalStateException("Invalid class name: " + className);
+ } catch (IllegalAccessException ex) {
+ throw new IllegalAccessError(ex.getMessage());
+ } catch (InstantiationException ex) {
+ throw new InstantiationError(ex.getMessage());
+ }
+ }
+ if (factory != null) {
+ connManager = factory.newInstance(params, registry);
+ } else {
+ connManager = new SingleClientConnManager(registry);
+ }
- return connManager;
- }
+ return connManager;
+ }
}
View
60 src/main/java/org/villane/shttpc/package.scala
@@ -1,30 +1,30 @@
-package org.villane
-
-import xml._
-import xml.factory.XMLLoader
-import org.apache.http.NameValuePair
-import org.apache.http.message.BasicNameValuePair
-
-package object shttpc {
- var DefaultHtmlLoader: XMLLoader[Elem] = NekoHTML
-
- implicit def richHttpResponse(r: SimpleHttpResponse) = new RichHttpResponse(r)
-
- implicit def scala2javaMap[K,V](sm: Map[K,V]): java.util.Map[K,V] = {
- val jm = new java.util.HashMap[K,V]
- for ((k,v) <- sm) jm.put(k,v)
- jm
- }
-
- implicit def scalaMap2nameValuePair(sm: Map[String,String]): java.util.List[NameValuePair] = {
- val nvp = new java.util.ArrayList[NameValuePair]
- for ((k,v) <- sm) nvp.add(new BasicNameValuePair(k,v))
- nvp
- }
-
- implicit def site2url(site: Site) = site.url
- implicit def url2site(url: String) = Site(url)
-
- implicit def betterXPath(node: NodeSeq) = BetterXPath.betterXPath(node)
-
-}
+package org.villane
+
+import xml._
+import xml.factory.XMLLoader
+import org.apache.http.NameValuePair
+import org.apache.http.message.BasicNameValuePair
+
+package object shttpc {
+ var DefaultHtmlLoader: XMLLoader[Elem] = NekoHTML
+
+ implicit def richHttpResponse(r: SimpleHttpResponse) = new RichHttpResponse(r)
+
+ implicit def scala2javaMap[K, V](sm: Map[K, V]): java.util.Map[K, V] = {
+ val jm = new java.util.HashMap[K, V]
+ for ((k, v) <- sm) jm.put(k, v)
+ jm
+ }
+
+ implicit def scalaMap2nameValuePair(sm: Map[String, String]): java.util.List[NameValuePair] = {
+ val nvp = new java.util.ArrayList[NameValuePair]
+ for ((k, v) <- sm) nvp.add(new BasicNameValuePair(k, v))
+ nvp
+ }
+
+ implicit def site2url(site: Site) = site.url
+ implicit def url2site(url: String) = Site(url)
+
+ implicit def betterXPath(node: NodeSeq) = BetterXPath.betterXPath(node)
+
+}

0 comments on commit 8650935

Please sign in to comment.