Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

TFJ-630 contributor API support

removed:
StatusUpdate#setContributingTo(long)
configuration parameter added:
twitter4j.contributingTo
  • Loading branch information...
commit 45500f134a36991810457048c2fdd8dac6cbae60 1 parent 44b574d
Yusuke Yamamoto authored
37 twitter4j-core/src/main/java/twitter4j/StatusUpdate.java
@@ -39,7 +39,6 @@
39 39 private String mediaName;
40 40 private transient InputStream mediaBody;
41 41 private File mediaFile;
42   - private long contributingTo = -1L;
43 42 private static final long serialVersionUID = -3595502688477609916L;
44 43
45 44 public StatusUpdate(String status) {
@@ -138,39 +137,6 @@ public StatusUpdate media(String name, InputStream body) {
138 137 }
139 138
140 139 /**
141   - * Sets the user id contributing to
142   - *
143   - * @param contributingTo the user id contributing to
144   - * @see <a href="http://groups.google.com/group/twitter-api-announce/browse_thread/thread/12273c2d03c1b606">Developer Preview: Contributor API</a>
145   - * @since Twitter4J 3.0.0
146   - */
147   - public void setContributingTo(long contributingTo) {
148   - this.contributingTo = contributingTo;
149   - }
150   -
151   - /**
152   - * Sets the user id contributing to and returns this instance
153   - *
154   - * @param contributingTo the user id contributing to
155   - * @see <a href="http://groups.google.com/group/twitter-api-announce/browse_thread/thread/12273c2d03c1b606">Developer Preview: Contributor API</a>
156   - * @since Twitter4J 3.0.0
157   - */
158   - public StatusUpdate contributingTo(long contributingTo) {
159   - this.contributingTo = contributingTo;
160   - return this;
161   - }
162   -
163   - /**
164   - * Returns the user id contributing to
165   - *
166   - * @see <a href="http://groups.google.com/group/twitter-api-announce/browse_thread/thread/12273c2d03c1b606">Developer Preview: Contributor API</a>
167   - * @since Twitter4J 3.0.0
168   - */
169   - public long getContributingTo() {
170   - return this.contributingTo;
171   - }
172   -
173   - /**
174 140 * @since Twitter4J 2.2.5
175 141 */
176 142 public void setPossiblySensitive(boolean possiblySensitive) {
@@ -242,9 +208,6 @@ public StatusUpdate annotation(Annotation annotation) {
242 208 params.add(new HttpParameter("media[]", mediaName, mediaBody));
243 209 params.add(new HttpParameter("possibly_sensitive", possiblySensitive));
244 210 }
245   - if (-1 != contributingTo) {
246   - appendParameter("contributingto", contributingTo, params);
247   - }
248 211 HttpParameter[] paramArray = new HttpParameter[params.size()];
249 212 return params.toArray(paramArray);
250 213 }
45 twitter4j-core/src/main/java/twitter4j/TwitterImpl.java
@@ -49,9 +49,15 @@
49 49 TwitterImpl(Configuration conf, Authorization auth) {
50 50 super(conf, auth);
51 51 INCLUDE_MY_RETWEET = new HttpParameter("include_my_retweet", 1);
  52 + if (conf.getContributingTo() != -1L) {
  53 + CONTRIBUTING_TO = new HttpParameter("contributingto", conf.getContributingTo());
  54 + } else {
  55 + CONTRIBUTING_TO = null;
  56 + }
52 57 }
53 58
54 59 private final HttpParameter INCLUDE_MY_RETWEET;
  60 + private final HttpParameter CONTRIBUTING_TO;
55 61
56 62 private HttpParameter[] mergeParameters(HttpParameter[] params1, HttpParameter[] params2) {
57 63 if (params1 != null && params2 != null) {
@@ -1753,6 +1759,13 @@ public TwitterAPIConfiguration getAPIConfiguration() throws TwitterException {
1753 1759 }
1754 1760
1755 1761 private HttpResponse get(String url) throws TwitterException {
  1762 + if (CONTRIBUTING_TO != null) {
  1763 + if (url.contains("?")) {
  1764 + url = url + "&contributingto=" + conf.getContributingTo();
  1765 + } else {
  1766 + url = url + "?contributingto=" + conf.getContributingTo();
  1767 + }
  1768 + }
1756 1769 if (!conf.isMBeanEnabled()) {
1757 1770 return http.get(url, auth);
1758 1771 } else {
@@ -1769,15 +1782,15 @@ private HttpResponse get(String url) throws TwitterException {
1769 1782 }
1770 1783 }
1771 1784
1772   - private HttpResponse get(String url, HttpParameter[] parameters) throws TwitterException {
  1785 + private HttpResponse get(String url, HttpParameter[] params) throws TwitterException {
1773 1786 if (!conf.isMBeanEnabled()) {
1774   - return http.get(url, parameters, auth);
  1787 + return http.get(url, mergeContributingTo(params), auth);
1775 1788 } else {
1776 1789 // intercept HTTP call for monitoring purposes
1777 1790 HttpResponse response = null;
1778 1791 long start = System.currentTimeMillis();
1779 1792 try {
1780   - response = http.get(url, parameters, auth);
  1793 + response = http.get(url, mergeContributingTo(params), auth);
1781 1794 } finally {
1782 1795 long elapsedTime = System.currentTimeMillis() - start;
1783 1796 TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
@@ -1787,6 +1800,9 @@ private HttpResponse get(String url, HttpParameter[] parameters) throws TwitterE
1787 1800 }
1788 1801
1789 1802 private HttpResponse post(String url) throws TwitterException {
  1803 + if (CONTRIBUTING_TO != null) {
  1804 + return post(url, new HttpParameter[0]);
  1805 + }
1790 1806 if (!conf.isMBeanEnabled()) {
1791 1807 return http.post(url, auth);
1792 1808 } else {
@@ -1803,15 +1819,15 @@ private HttpResponse post(String url) throws TwitterException {
1803 1819 }
1804 1820 }
1805 1821
1806   - private HttpResponse post(String url, HttpParameter[] parameters) throws TwitterException {
  1822 + private HttpResponse post(String url, HttpParameter[] params) throws TwitterException {
1807 1823 if (!conf.isMBeanEnabled()) {
1808   - return http.post(url, parameters, auth);
  1824 + return http.post(url, mergeContributingTo(params), auth);
1809 1825 } else {
1810 1826 // intercept HTTP call for monitoring purposes
1811 1827 HttpResponse response = null;
1812 1828 long start = System.currentTimeMillis();
1813 1829 try {
1814   - response = http.post(url, parameters, auth);
  1830 + response = http.post(url, mergeContributingTo(params), auth);
1815 1831 } finally {
1816 1832 long elapsedTime = System.currentTimeMillis() - start;
1817 1833 TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
@@ -1820,20 +1836,11 @@ private HttpResponse post(String url, HttpParameter[] parameters) throws Twitter
1820 1836 }
1821 1837 }
1822 1838
1823   - private HttpResponse delete(String url) throws TwitterException {
1824   - if (!conf.isMBeanEnabled()) {
1825   - return http.delete(url, auth);
  1839 + private HttpParameter[] mergeContributingTo(HttpParameter[] params) {
  1840 + if (CONTRIBUTING_TO == null) {
  1841 + return params;
1826 1842 } else {
1827   - // intercept HTTP call for monitoring purposes
1828   - HttpResponse response = null;
1829   - long start = System.currentTimeMillis();
1830   - try {
1831   - response = http.delete(url, auth);
1832   - } finally {
1833   - long elapsedTime = System.currentTimeMillis() - start;
1834   - TwitterAPIMonitor.getInstance().methodCalled(url, elapsedTime, isOk(response));
1835   - }
1836   - return response;
  1843 + return mergeParameters(params, CONTRIBUTING_TO);
1837 1844 }
1838 1845 }
1839 1846
2  twitter4j-core/src/main/java/twitter4j/conf/Configuration.java
@@ -120,5 +120,7 @@
120 120
121 121 int getAsyncNumThreads();
122 122
  123 + long getContributingTo();
  124 +
123 125 String getDispatcherImpl();
124 126 }
11 twitter4j-core/src/main/java/twitter4j/conf/ConfigurationBase.java
@@ -70,6 +70,7 @@
70 70
71 71 private int asyncNumThreads;
72 72
  73 + private long contributingTo;
73 74 private boolean includeRTsEnabled = true;
74 75
75 76 private boolean includeEntitiesEnabled = true;
@@ -156,6 +157,7 @@ protected ConfigurationBase() {
156 157 setOAuthAccessToken(null);
157 158 setOAuthAccessTokenSecret(null);
158 159 setAsyncNumThreads(1);
  160 + setContributingTo(-1L);
159 161 setClientVersion(Version.getVersion());
160 162 setClientURL("http://twitter4j.org/en/twitter4j-" + Version.getVersion() + ".xml");
161 163 setUserAgent("twitter4j http://twitter4j.org/ /" + Version.getVersion());
@@ -475,6 +477,15 @@ protected final void setAsyncNumThreads(int asyncNumThreads) {
475 477 }
476 478
477 479 @Override
  480 + public final long getContributingTo() {
  481 + return contributingTo;
  482 + }
  483 +
  484 + protected final void setContributingTo(long contributingTo) {
  485 + this.contributingTo = contributingTo;
  486 + }
  487 +
  488 + @Override
478 489 public final String getClientVersion() {
479 490 return clientVersion;
480 491 }
6 twitter4j-core/src/main/java/twitter4j/conf/ConfigurationBuilder.java
@@ -217,6 +217,12 @@ public ConfigurationBuilder setAsyncNumThreads(int asyncNumThreads) {
217 217 return this;
218 218 }
219 219
  220 + public ConfigurationBuilder setContributingTo(long contributingTo) {
  221 + checkNotBuilt();
  222 + configurationBean.setContributingTo(contributingTo);
  223 + return this;
  224 + }
  225 +
220 226 public ConfigurationBuilder setClientVersion(String clientVersion) {
221 227 checkNotBuilt();
222 228 configurationBean.setClientVersion(clientVersion);
13 twitter4j-core/src/main/java/twitter4j/conf/PropertyConfiguration.java
@@ -72,6 +72,7 @@
72 72 public static final String UPLOAD_BASE_URL = "uploadBaseURL";
73 73
74 74 public static final String ASYNC_NUM_THREADS = "async.numThreads";
  75 + public static final String CONTRIBUTING_TO = "contributingTo";
75 76 public static final String ASYNC_DISPATCHER_IMPL = "async.dispatcherImpl";
76 77 public static final String INCLUDE_RTS = "includeRTs";
77 78 public static final String INCLUDE_ENTITIES = "includeEntities";
@@ -291,6 +292,9 @@ private void setFieldsWithPrefix(Properties props, String prefix) {
291 292 if (notNull(props, prefix, ASYNC_NUM_THREADS)) {
292 293 setAsyncNumThreads(getIntProperty(props, prefix, ASYNC_NUM_THREADS));
293 294 }
  295 + if (notNull(props, prefix, CONTRIBUTING_TO)) {
  296 + setContributingTo(getLongProperty(props, prefix, CONTRIBUTING_TO));
  297 + }
294 298 if (notNull(props, prefix, ASYNC_DISPATCHER_IMPL)) {
295 299 setDispatcherImpl(getString(props, prefix, ASYNC_DISPATCHER_IMPL));
296 300 }
@@ -390,6 +394,15 @@ protected int getIntProperty(Properties props, String prefix, String name) {
390 394 }
391 395 }
392 396
  397 + protected long getLongProperty(Properties props, String prefix, String name) {
  398 + String value = props.getProperty(prefix + name);
  399 + try {
  400 + return Long.parseLong(value);
  401 + } catch (NumberFormatException nfe) {
  402 + return -1L;
  403 + }
  404 + }
  405 +
393 406 protected String getString(Properties props, String prefix, String name) {
394 407 return props.getProperty(prefix + name);
395 408 }

0 comments on commit 45500f1

Please sign in to comment.
Something went wrong with that request. Please try again.