Change Pastebin API to handle private and unlisted paste

This commit is contained in:
Nicolas VINOT 2012-02-24 14:08:07 +01:00
parent 2e0f8fe90e
commit 06acb66d12
9 changed files with 139 additions and 64 deletions

View File

@ -4,16 +4,12 @@
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/> <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/> <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="src" output="target/classes" path="target/generated-sources/jaxb"> <classpathentry kind="src" output="target/classes" path="target/generated-sources/jaxb">
<attributes> <attributes>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

View File

@ -39,7 +39,11 @@
value="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" /> value="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
</bean> </bean>
<bean id="job" class="fr.imirhil.april.hebdobot.Job" /> <bean id="job" class="fr.imirhil.april.hebdobot.Job">
<property name="statusNetMessage"
value="Revue hebdomadaire !april dans %dmin sur irc://irc.freenode.org/april ou http://webchat.freenode.net/?channels=april" />
<property name="ircMessage" value="Revue hebdomadaire dans %dmin" />
</bean>
<bean id="trigger30" class="org.springframework.scheduling.quartz.CronTriggerBean"> <bean id="trigger30" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail"> <property name="jobDetail">

37
pom.xml
View File

@ -10,6 +10,8 @@
<maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target> <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<project.build.sourceEncoding>UTF8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
<slf4j.version>1.6.4</slf4j.version>
<spring.version>3.1.1.RELEASE</spring.version>
</properties> </properties>
<build> <build>
<plugins> <plugins>
@ -48,6 +50,15 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<localCheckout>true</localCheckout>
<pushChanges>false</pushChanges>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
@ -59,12 +70,12 @@
<dependency> <dependency>
<groupId>joda-time</groupId> <groupId>joda-time</groupId>
<artifactId>joda-time</artifactId> <artifactId>joda-time</artifactId>
<version>2.0</version> <version>2.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>2.0.1</version> <version>2.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
@ -74,56 +85,56 @@
<dependency> <dependency>
<groupId>javax.xml.bind</groupId> <groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId> <artifactId>jaxb-api</artifactId>
<version>2.1</version> <version>2.2.6</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>opensymphony</groupId> <groupId>opensymphony</groupId>
<artifactId>quartz</artifactId> <artifactId>quartz</artifactId>
<version>1.6.2</version> <version>1.6.5</version>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
<version>4.1.2</version> <version>4.1.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.8.2</version> <version>4.10</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>1.6.1</version> <version>${slf4j.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId> <artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version> <version>${slf4j.version}</version>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.scribe</groupId> <groupId>org.scribe</groupId>
<artifactId>scribe</artifactId> <artifactId>scribe</artifactId>
<version>1.2.3</version> <version>1.3.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId> <artifactId>spring-context</artifactId>
<version>3.0.6.RELEASE</version> <version>${spring.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
<version>3.0.6.RELEASE</version> <version>${spring.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId> <artifactId>spring-context-support</artifactId>
<version>3.0.6.RELEASE</version> <version>${spring.version}</version>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
@ -135,7 +146,7 @@
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId> <artifactId>spring-tx</artifactId>
<version>3.0.6.RELEASE</version> <version>${spring.version}</version>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -13,6 +13,7 @@ import org.springframework.context.support.FileSystemXmlApplicationContext;
import fr.imirhil.april.hebdobot.irc.Bot; import fr.imirhil.april.hebdobot.irc.Bot;
import fr.imirhil.april.hebdobot.irc.ReviewListener; import fr.imirhil.april.hebdobot.irc.ReviewListener;
import fr.imirhil.april.hebdobot.pastebin.PastebinClient; import fr.imirhil.april.hebdobot.pastebin.PastebinClient;
import fr.imirhil.april.hebdobot.pastebin.Private;
import fr.imirhil.april.hebdobot.review.Review; import fr.imirhil.april.hebdobot.review.Review;
public class Application implements ReviewListener { public class Application implements ReviewListener {
@ -36,7 +37,8 @@ public class Application implements ReviewListener {
final String text = review.toString(); final String text = review.toString();
try { try {
this.bot.sendMessage("Compte-rendu de la revue : " this.bot.sendMessage("Compte-rendu de la revue : "
+ this.pastebinClient.paste(text, "Revue APRIL " + date)); + this.pastebinClient.paste(text, "Revue APRIL " + date,
Private.UNLISTED));
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.error("Error during Pastebin submit", e); LOGGER.error("Error during Pastebin submit", e);
} }
@ -56,7 +58,7 @@ public class Application implements ReviewListener {
} }
public static void main(final String[] args) throws Exception { public static void main(final String[] args) throws Exception {
new FileSystemXmlApplicationContext("conf.xml"); new FileSystemXmlApplicationContext("conf.xml").registerShutdownHook();
new Application(); new Application();
} }
} }

View File

@ -3,6 +3,7 @@ package fr.imirhil.april.hebdobot;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
@ -19,7 +20,10 @@ public final class Context implements ApplicationContextAware {
return CONTEXT.getBean(clazz); return CONTEXT.getBean(clazz);
} }
public static void load() { public static void close() {
if (CONTEXT instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext)CONTEXT).close();
}
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -10,21 +10,31 @@ public class Job {
private Bot bot; private Bot bot;
@Resource @Resource
private IdentiCaClient identiCaClient; private IdentiCaClient identiCaClient;
private String statusNetMessage;
private String ircMessage;
private void notify(final String message) { public void setStatusNetMessage(String message) {
this.bot.sendMessage(message); this.statusNetMessage = message;
this.identiCaClient.post(message); }
public void setIrcMessage(String message) {
this.ircMessage = message;
}
private void notify(final int min) {
this.bot.sendMessage(String.format(this.ircMessage, min));
this.identiCaClient.post(String.format(this.statusNetMessage, min));
} }
public void at30() { public void at30() {
this.notify("Revue hebdomadaire dans 30min !"); this.notify(30);
} }
public void at45() { public void at45() {
this.notify("Revue hebdomadaire dans 15min !"); this.notify(15);
} }
public void at55() { public void at55() {
this.notify("Revue hebdomadaire dans 5min !"); this.notify(5);
} }
} }

View File

@ -5,6 +5,7 @@ import java.util.LinkedList;
import org.jibble.pircbot.PircBot; import org.jibble.pircbot.PircBot;
import fr.imirhil.april.hebdobot.Context;
import fr.imirhil.april.hebdobot.review.CollectiveTopic; import fr.imirhil.april.hebdobot.review.CollectiveTopic;
import fr.imirhil.april.hebdobot.review.IndividualTopic; import fr.imirhil.april.hebdobot.review.IndividualTopic;
import fr.imirhil.april.hebdobot.review.Message; import fr.imirhil.april.hebdobot.review.Message;
@ -100,7 +101,7 @@ public class Bot extends PircBot {
return false; return false;
} }
this.close(); Context.close();
return true; return true;
} }

View File

@ -68,14 +68,79 @@ public class PastebinClient {
this.apiUserKey = content; this.apiUserKey = content;
} }
public String paste(final String code) throws Exception {
return this.paste(code, null, Format.NONE, Private.PUBLIC,
Expiration.DAY_1);
}
public String paste(final String code, final Private privat)
throws Exception {
return this.paste(code, null, Format.NONE, privat, Expiration.DAY_1);
}
public String paste(final String code, final String name) throws Exception {
return this.paste(code, name, Format.NONE, Private.PUBLIC,
Expiration.DAY_1);
}
public String paste(final String code, final String name, public String paste(final String code, final String name,
final Format format, final boolean privat, final Private privat) throws Exception {
return this.paste(code, name, Format.NONE, privat, Expiration.DAY_1);
}
public String paste(final String code, final String name,
final Format format) throws Exception {
return this.paste(code, name, format, Private.PUBLIC, Expiration.DAY_1);
}
public String paste(final String code, final String name,
final Format format, final Private privat) throws Exception {
return this.paste(code, name, format, privat, Expiration.DAY_1);
}
public String paste(final String code, final Format format)
throws Exception {
return this.paste(code, null, format, Private.PUBLIC, Expiration.DAY_1);
}
public String paste(final String code, final Format format,
final Private privat) throws Exception {
return this.paste(code, null, format, privat, Expiration.DAY_1);
}
public String paste(final String code, final Expiration expiration)
throws Exception {
return this.paste(code, null, Format.NONE, Private.PUBLIC, expiration);
}
public String paste(final String code, final Private privat,
final Expiration expiration) throws Exception {
return this.paste(code, null, Format.NONE, privat, expiration);
}
public String paste(final String code, final Format format,
final Expiration expiration) throws Exception {
return this.paste(code, null, format, Private.PUBLIC, expiration);
}
public String paste(final String code, final Format format,
final Private privat, final Expiration expiration) throws Exception {
return this.paste(code, null, format, privat, expiration);
}
public String paste(final String code, final String name,
final Format format, final Expiration expiration) throws Exception {
return this.paste(code, name, format, Private.PUBLIC, expiration);
}
public String paste(final String code, final String name,
final Format format, final Private privat,
final Expiration expiration) throws Exception { final Expiration expiration) throws Exception {
final List<NameValuePair> params = new LinkedList<NameValuePair>(); final List<NameValuePair> params = new LinkedList<NameValuePair>();
setParameter(params, API_DEV_KEY, this.apiKey); setParameter(params, API_DEV_KEY, this.apiKey);
setParameter(params, API_USER_KEY, this.apiUserKey); setParameter(params, API_USER_KEY, this.apiUserKey);
setParameter(params, API_OPTION, Option.PASTE.getValue()); setParameter(params, API_OPTION, Option.PASTE.getValue());
setParameter(params, API_PASTE_PRIVATE, privat ? "1" : "0"); setParameter(params, API_PASTE_PRIVATE, privat.getValue());
setParameter(params, API_PASTE_NAME, name); setParameter(params, API_PASTE_NAME, name);
setParameter(params, API_PASTE_EXPIRATION, expiration.getValue()); setParameter(params, API_PASTE_EXPIRATION, expiration.getValue());
setParameter(params, API_PASTE_FORMAT, format.getValue()); setParameter(params, API_PASTE_FORMAT, format.getValue());
@ -92,39 +157,6 @@ public class PastebinClient {
return content; return content;
} }
public String paste(final String code) throws Exception {
return this.paste(code, null, Format.NONE, false, Expiration.DAY_1);
}
public String paste(final String code, final String name) throws Exception {
return this.paste(code, name, Format.NONE, false, Expiration.DAY_1);
}
public String paste(final String code, final String name,
final Format format) throws Exception {
return this.paste(code, name, format, false, Expiration.DAY_1);
}
public String paste(final String code, final Format format)
throws Exception {
return this.paste(code, null, format, false, Expiration.DAY_1);
}
public String paste(final String code, final Expiration expiration)
throws Exception {
return this.paste(code, null, Format.NONE, false, expiration);
}
public String paste(final String code, final Format format,
final Expiration expiration) throws Exception {
return this.paste(code, null, format, false, expiration);
}
public String paste(final String code, final String name,
final Format format, final Expiration expiration) throws Exception {
return this.paste(code, name, format, false, expiration);
}
private static void setParameter(final List<NameValuePair> params, private static void setParameter(final List<NameValuePair> params,
final String name, final String value) { final String name, final String value) {
if (value == null) { if (value == null) {

View File

@ -0,0 +1,15 @@
package fr.imirhil.april.hebdobot.pastebin;
public enum Private {
PUBLIC("0"), UNLISTED("1"), PRIVATE("2");
private String value;
private Private(final String value) {
this.value = value;
}
String getValue() {
return this.value;
}
}