Skip to content

Commit

Permalink
Fixed CS
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Jul 29, 2015
1 parent 5d9a2d9 commit 52fdb92
Show file tree
Hide file tree
Showing 23 changed files with 676 additions and 100 deletions.
30 changes: 23 additions & 7 deletions components/camel-atom/pom.xml
Expand Up @@ -47,6 +47,10 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.abdera</groupId>
<artifactId>abdera-core</artifactId>
Expand All @@ -66,10 +70,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.abdera</groupId>
<artifactId>abdera-parser</artifactId>
Expand Down Expand Up @@ -113,7 +113,6 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-api</artifactId>
Expand Down Expand Up @@ -182,7 +181,7 @@
<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -192,7 +191,24 @@
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty8-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
<version>${jetty8-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Expand Up @@ -25,36 +25,39 @@
import org.apache.camel.Processor;
import org.apache.camel.component.feed.EntryFilter;
import org.apache.camel.component.feed.FeedEntryPollingConsumer;
import org.apache.camel.util.ObjectHelper;

/**
* Consumer to poll atom feeds and return each entry from the feed step by step.
*
* @version
*/
public class AtomEntryPollingConsumer extends FeedEntryPollingConsumer {
private Document<Feed> document;

public AtomEntryPollingConsumer(AtomEndpoint endpoint, Processor processor, boolean filter, Date lastUpdate, boolean throttleEntries) {
super(endpoint, processor, filter, lastUpdate, throttleEntries);
}
}

private Document<Feed> getDocument() throws IOException, ParseException {
if (document == null) {
document = AtomUtils.parseDocument(endpoint.getFeedUri());
if (ObjectHelper.isEmpty(endpoint.getUsername()) || ObjectHelper.isEmpty(endpoint.getPassword())) {
document = AtomUtils.parseDocument(endpoint.getFeedUri());
} else {
document = AtomUtils.parseDocument(endpoint.getFeedUri(), endpoint.getUsername(), endpoint.getPassword());
}
Feed root = document.getRoot();
if (endpoint.isSortEntries()) {
sortEntries(root);
}
list = root.getEntries();
}
list = root.getEntries();
entryIndex = list.size() - 1;
}
return document;
}

protected void sortEntries(Feed feed) {
feed.sortEntriesByUpdated(true);
}

@Override
protected void populateList(Object feed) throws ParseException, IOException {
// list is populated already in the createFeed method
Expand All @@ -69,7 +72,7 @@ protected Object createFeed() throws IOException {
protected void resetList() {
document = null;
}

@Override
protected EntryFilter createEntryFilter(Date lastUpdate) {
return new UpdatedDateFilter(lastUpdate);
Expand Down
Expand Up @@ -22,11 +22,10 @@
import org.apache.abdera.model.Feed;
import org.apache.camel.Processor;
import org.apache.camel.component.feed.FeedPollingConsumer;
import org.apache.camel.util.ObjectHelper;

/**
* Consumer to poll atom feeds and return the full feed.
*
* @version
*/
public class AtomPollingConsumer extends FeedPollingConsumer {

Expand All @@ -36,7 +35,12 @@ public AtomPollingConsumer(AtomEndpoint endpoint, Processor processor) {

@Override
protected Object createFeed() throws IOException {
Document<Feed> document = AtomUtils.parseDocument(endpoint.getFeedUri());
Document<Feed> document;
if (ObjectHelper.isEmpty(endpoint.getUsername()) || ObjectHelper.isEmpty(endpoint.getPassword())) {
document = AtomUtils.parseDocument(endpoint.getFeedUri());
} else {
document = AtomUtils.parseDocument(endpoint.getFeedUri(), endpoint.getUsername(), endpoint.getPassword());
}
return document.getRoot();
}
}
Expand Up @@ -18,13 +18,15 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Feed;
import org.apache.abdera.parser.ParseException;
import org.apache.abdera.parser.Parser;
import org.apache.commons.codec.binary.Base64;

/**
* Atom utilities.
Expand All @@ -46,12 +48,25 @@ public static Parser getAtomParser() {
* Parses the given uri and returns the response as a atom feed document.
*
* @param uri the uri for the atom feed.
* @return the document
* @throws IOException is thrown if error reading from the uri
* @return the document
* @throws IOException is thrown if error reading from the uri
* @throws ParseException is thrown if the parsing failed
*/
public static Document<Feed> parseDocument(String uri) throws IOException, ParseException {
InputStream in = new URL(uri).openStream();
return parseInputStream(in);
}

public static Document<Feed> parseDocument(String uri, String username, String password) throws IOException {
URL feedUrl = new URL(uri);
HttpURLConnection httpcon = (HttpURLConnection) feedUrl.openConnection();
String encoding = Base64.encodeBase64String(username.concat(":").concat(password).getBytes());
httpcon.setRequestProperty("Authorization", "Basic " + encoding);
InputStream in = httpcon.getInputStream();
return parseInputStream(in);
}

private static Document<Feed> parseInputStream(InputStream in) throws ParseException {
Parser parser = getAtomParser();
// set the thread context loader with the ParserClassLoader
ClassLoader old = Thread.currentThread().getContextClassLoader();
Expand All @@ -61,7 +76,6 @@ public static Document<Feed> parseDocument(String uri) throws IOException, Parse
} finally {
Thread.currentThread().setContextClassLoader(old);
}

}

}
Expand Up @@ -47,6 +47,10 @@ public abstract class FeedEndpoint extends DefaultPollingEndpoint {
private boolean sortEntries;
@UriParam(defaultValue = "true")
private boolean throttleEntries = true;
@UriParam
private String username;
@UriParam
private String password;

public FeedEndpoint() {
}
Expand Down Expand Up @@ -208,6 +212,36 @@ public void setThrottleEntries(boolean throttleEntries) {
public boolean isThrottleEntries() {
return this.throttleEntries;
}

/**
* @return the username
*/
public String getUsername() {
return username;
}

/**
* Sets the username to be used for basic authentication
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}

/**
* @return the password
*/
public String getPassword() {
return password;
}

/**
* Sets the password to be used for basic authentication
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}

// Implementation methods
//-------------------------------------------------------------------------
Expand Down
Expand Up @@ -17,7 +17,6 @@
package org.apache.camel.component.atom;

import java.text.SimpleDateFormat;

import javax.naming.Context;

import org.apache.camel.builder.RouteBuilder;
Expand Down Expand Up @@ -59,13 +58,13 @@ protected Context createJndiContext() throws Exception {
jndi.bind("myDate", df.parse("2007-11-13 14:35:00 +0100"));
return jndi;
}

protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
from("atom:file:src/test/data/feed.atom?splitEntries=true&consumer.delay=500").to("mock:result1");

from("atom:file:src/test/data/feed.atom?splitEntries=true&filter=false&consumer.delay=500").to("mock:result2");
from("atom:file:src/test/data/feed.atom?splitEntries=true&filter=false&consumer.delay=500").to("mock:result2");

from("atom:file:src/test/data/feed.atom?splitEntries=true&filter=true&lastUpdate=#myDate&consumer.delay=500").to("mock:result3");
}
Expand Down
@@ -0,0 +1,51 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.atom;

import org.apache.camel.builder.RouteBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;

public class AtomEntryPollingConsumerWithBasicAuthTest extends AtomEntryPollingConsumerTest {

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("atom:http://localhost:" + JettyTestServer.getInstance().port + "/?splitEntries=true&consumer.delay=500&username=camel&password=camelPass")
.to("mock:result1");

from("atom:http://localhost:" + JettyTestServer.getInstance().port + "/?splitEntries=true&filter=false&consumer.delay=500&username=camel&password=camelPass")
.to("mock:result2");

from("atom:http://localhost:" + JettyTestServer.getInstance().port + "/?splitEntries=true&filter=true&lastUpdate=#myDate&consumer.delay=500&username=camel&password=camelPass")
.to("mock:result3");
}
};
}

@BeforeClass
public static void startServer() {
JettyTestServer.getInstance().startServer();
}

@AfterClass
public static void stopServer() {
JettyTestServer.getInstance().stopServer();
}
}
Expand Up @@ -17,7 +17,6 @@
package org.apache.camel.component.atom;

import java.util.Date;

import javax.naming.Context;

import org.apache.abdera.model.Entry;
Expand All @@ -32,7 +31,7 @@
public class AtomEntrySortTest extends CamelTestSupport {

@Test
public void testSortedEntries() throws Exception {
public void testSortedEntries() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:sorted");
mock.expectsAscending(ExpressionBuilder.beanExpression("myBean", "getPubDate"));
mock.expectedMessageCount(10);
Expand All @@ -41,21 +40,21 @@ public void testSortedEntries() throws Exception {
}

@Test
public void testUnSortedEntries() throws Exception {
public void testUnSortedEntries() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:unsorted");
mock.expectsAscending(ExpressionBuilder.beanExpression("myBean", "getPubDate"));
mock.expectedMessageCount(10);
mock.setResultWaitTime(2000L);
mock.assertIsNotSatisfied(2000L);
}
}

@Override
protected Context createJndiContext() throws Exception {
JndiContext jndi = new JndiContext();
jndi.bind("myBean", new MyBean());
return jndi;
}

protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
Expand All @@ -64,11 +63,11 @@ public void configure() throws Exception {
}
};
}

public static class MyBean {
public Date getPubDate(@Body Object body) {
Entry syndEntry = (Entry) body;
return syndEntry.getUpdated();
return syndEntry.getUpdated();
}
}
}
Expand Up @@ -20,7 +20,7 @@
import org.junit.Test;

/**
* @version
* @version
*/
public class AtomHttpNoCamelParametersTest extends CamelTestSupport {

Expand Down

0 comments on commit 52fdb92

Please sign in to comment.