Skip to content
This repository has been archived by the owner on Jul 9, 2022. It is now read-only.

Commit

Permalink
Feed operations
Browse files Browse the repository at this point in the history
  • Loading branch information
habuma committed Mar 28, 2012
1 parent ff54d96 commit 0495039
Show file tree
Hide file tree
Showing 17 changed files with 679 additions and 1 deletion.
@@ -0,0 +1,68 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed 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.springframework.mobile.urbanairship;

import java.util.Date;

public class Feed {

private final String id;

private final String url;

private final String feedUrl;

private final Date lastChecked;

private final FeedNotificationTemplate template;

private final boolean broadcast;

private Feed(String id, String url, String feedUrl, Date lastChecked, FeedNotificationTemplate template, boolean broadcast) {
this.id = id;
this.url = url;
this.feedUrl = feedUrl;
this.lastChecked = lastChecked;
this.template = template;
this.broadcast = broadcast;
}

public String getId() {
return id;
}

public String getUrl() {
return url;
}

public String getFeedUrl() {
return feedUrl;
}

public Date getLastChecked() {
return lastChecked;
}

public FeedNotificationTemplate getTemplate() {
return template;
}

public boolean isBroadcast() {
return broadcast;
}

}

@@ -0,0 +1,150 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed 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.springframework.mobile.urbanairship;

import java.util.Map;


public class FeedConfig {

private String url;

private Boolean broadcast;

private FeedNotificationTemplate template;

public String getUrl() {
return url;
}

public Boolean isBroadcast() {
return broadcast;
}

public FeedNotificationTemplate getTemplate() {
return template;
}

private FeedConfig(String url) {
this.url = url;
}

public static FeedConfigBuilder builder(String url, String messageTemplate) {
return new FeedConfigBuilder(url, messageTemplate);
}

public static class FeedConfigBuilder {
private String url;
private String messageTemplate;
private String sound;
private Integer badgeCount;
private String autoBadge;
private Boolean broadcast;
private Map<String, String> extra;

public FeedConfigBuilder(String url, String messageTemplate) {
this.url = url;
this.messageTemplate = messageTemplate;
}

/**
* Sets the badge to a specific value.
* @param badgeCount the value to set the badge to
*/
public FeedConfigBuilder badge(int badgeCount) {
this.autoBadge = null;
this.badgeCount = badgeCount;
return this;
}

/**
* Sets the badge to "auto" to enable auto-badging.
*/
public FeedConfigBuilder autoBadge() {
this.badgeCount = null;
this.autoBadge = "auto";
return this;
}

/**
* Increments the badge by the given value.
* @param count the amount to increment the badge by.
*/
public FeedConfigBuilder badgeIncrement(int count) {
this.badgeCount = null;
this.autoBadge = "+" + count;
return this;
}

/**
* Decrements the badge by the given value.
* @param count the amount to decrement the badge by.
*/
public FeedConfigBuilder badgeDecrement(int count) {
this.badgeCount = null;
this.autoBadge = "-" + count;
return this;
}

public FeedConfigBuilder broadcast(boolean broadcast) {
this.broadcast = broadcast;
return this;
}

public FeedConfigBuilder sound(String sound) {
this.sound = sound;
return this;
}

public FeedConfigBuilder extra(Map<String, String> extra) {
this.extra = extra;
return this;
}


public FeedConfig build() {
FeedConfig feedConfig = new FeedConfig(url);
feedConfig.broadcast = broadcast;
feedConfig.template = new FeedNotificationTemplate(createAps(), createAndroid(), createBlackberry());
return feedConfig;
}

private Aps createAps() {
Aps aps = new Aps(messageTemplate);
aps.setSound(sound);
if (badgeCount != null) {
aps.setBadge(badgeCount);
} else if (autoBadge != null) {
aps.setBadge(autoBadge);
}
return aps;
}

private Android createAndroid() {
Android android = new Android(messageTemplate);
if (extra != null) {
android.setExtra(extra);
}
return android;
}

private Blackberry createBlackberry() {
return new Blackberry(messageTemplate);
}

}

}
@@ -0,0 +1,49 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed 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.springframework.mobile.urbanairship;

import org.codehaus.jackson.annotate.JsonProperty;

public class FeedNotificationTemplate {

@JsonProperty("aps")
private Aps aps;

@JsonProperty("android")
private Android android;

@JsonProperty("blackberry")
private Blackberry blackberry;

public FeedNotificationTemplate(Aps aps, Android android, Blackberry blackberry) {
this.aps = aps;
this.android = android;
this.blackberry = blackberry;
}

public Aps getAps() {
return aps;
}

public Android getAndroid() {
return android;
}

public Blackberry getBlackberry() {
return blackberry;
}

}
@@ -0,0 +1,32 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed 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.springframework.mobile.urbanairship;

import java.util.List;

public interface FeedOperations {

Feed createFeed(FeedConfig feedConfig);

Feed getFeed(String feedId);

List<Feed> getFeeds();

void updateFeed(String feedId, FeedConfig feedConfig);

void deleteFeed(String feedId);

}
Expand Up @@ -19,6 +19,8 @@

public interface UrbanAirship {

FeedOperations feedOperations();

PartnerOperations partnerOperations();

PushOperations pushOperations();
Expand Down
@@ -0,0 +1,68 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed 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.springframework.mobile.urbanairship.impl;

import static org.springframework.mobile.urbanairship.impl.UrbanAirshipTemplate.*;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.mobile.urbanairship.Feed;
import org.springframework.mobile.urbanairship.FeedConfig;
import org.springframework.mobile.urbanairship.FeedOperations;
import org.springframework.web.client.RestOperations;

public class FeedTemplate implements FeedOperations {

private final RestOperations restOperations;

public FeedTemplate(RestOperations restOperations) {
this.restOperations = restOperations;
}

public Feed createFeed(FeedConfig feedConfig) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<FeedConfig> requestEntity = new HttpEntity<FeedConfig>(feedConfig, headers);
return restOperations.exchange(FEED_URL, HttpMethod.POST, requestEntity, Feed.class).getBody();
}

public Feed getFeed(String feedId) {
return restOperations.getForObject(FEED_URL + "/" + feedId, Feed.class);
}

public List<Feed> getFeeds() {
return restOperations.getForObject(FEED_URL, FeedList.class);
}

public void updateFeed(String feedId, FeedConfig feedConfig) {
restOperations.put(FEED_URL + "/" + feedId, feedConfig);
}

public void deleteFeed(String feedId) {
restOperations.delete(FEED_URL + "/" + feedId);
}

private static final String FEED_URL = URBANAIRSHIP_API_URL + "feeds";

@SuppressWarnings("serial")
private static class FeedList extends ArrayList<Feed> {}

}

0 comments on commit 0495039

Please sign in to comment.