001/* 002 * flattr4j - A Java library for Flattr 003 * 004 * Copyright (C) 2012 Richard "Shred" Körber 005 * http://flattr4j.shredzone.org 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License / GNU Lesser 009 * General Public License as published by the Free Software Foundation, 010 * either version 3 of the License, or (at your option) any later version. 011 * 012 * Licensed under the Apache License, Version 2.0 (the "License"); 013 * you may not use this file except in compliance with the License. 014 * 015 * This program is distributed in the hope that it will be useful, 016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 018 */ 019package org.shredzone.flattr4j.model; 020 021import java.io.UnsupportedEncodingException; 022import java.net.URLEncoder; 023 024import org.shredzone.flattr4j.FlattrService; 025 026/** 027 * An {@link AutoSubmission} is used to generate a URL for auto submission. A user must 028 * be set, which will be the owner of the Thing. 029 * 030 * @author Richard "Shred" Körber 031 * @since 2.0 032 */ 033public class AutoSubmission extends Submission implements UserId, UserIdentifier { 034 private static final long serialVersionUID = 469255989509420133L; 035 036 private static final String ENCODING = "utf-8"; 037 038 private String userId; 039 private UserIdentifier identifier; 040 041 /** 042 * The user to submit this Submission on behalf of. 043 */ 044 public UserId getUser() { return userId != null ? User.withId(userId) : null; } 045 public void setUser(UserId user) { this.userId = user.getUserId(); } 046 047 @Override 048 public String getUserId() { return userId; } 049 050 /** 051 * The user identifier of the local user 052 * <p> 053 * This is only available for Partner Site Integration. You need to register your 054 * site with Flattr in order to use user identifiers. 055 */ 056 public UserIdentifier getIdentifier() { return identifier; } 057 public void setIdentifier(UserIdentifier identifier) { this.identifier = identifier; } 058 059 @Override 060 public String getUserIdentifier() { return identifier != null ? identifier.getUserIdentifier() : null; } 061 062 /** 063 * Returns a URL that can be used for submitting a Thing (for example in a link). 064 * <p> 065 * Use this URL as a fallback only if you can't use 066 * {@link FlattrService#create(Submission)} or JavaScript. The length of an URL is 067 * limited and depends on the browser and the server. The submission will be truncated 068 * if the maximum length was exceeded. 069 * 070 * @return Submission URL 071 */ 072 public String toUrl() { 073 if (userId == null && identifier == null) { 074 throw new IllegalArgumentException("Anonymous submissions are not allowed"); 075 } 076 if (userId != null && identifier != null) { 077 throw new IllegalArgumentException("Either user or identifier must be set, but not both"); 078 } 079 080 try { 081 StringBuilder sb = new StringBuilder(); 082 sb.append("https://flattr.com/submit/auto"); 083 084 if (getUserId() != null) { 085 sb.append("?user_id=").append(URLEncoder.encode(getUserId(), ENCODING)); 086 } else { 087 sb.append("?owner=").append(URLEncoder.encode(getUserIdentifier(), ENCODING)); 088 } 089 sb.append("&url=").append(URLEncoder.encode(getUrl(), ENCODING)); 090 091 if (getCategory() != null) { 092 sb.append("&category=").append(URLEncoder.encode(getCategory().getCategoryId(), ENCODING)); 093 } 094 095 if (getLanguage() != null) { 096 sb.append("&language=").append(URLEncoder.encode(getLanguage().getLanguageId(), ENCODING)); 097 } 098 099 if (getTitle() != null) { 100 sb.append("&title=").append(URLEncoder.encode(getTitle(), ENCODING)); 101 } 102 103 if (isHidden() != null && isHidden().booleanValue() == true) { 104 sb.append("&hidden=1"); 105 } 106 107 if (getTags() != null && !getTags().isEmpty()) { 108 sb.append("&tags=").append(URLEncoder.encode(getTagsAsString(), ENCODING)); 109 } 110 111 if (getDescription() != null) { 112 sb.append("&description=").append(URLEncoder.encode(getDescription(), ENCODING)); 113 } 114 115 return sb.toString(); 116 } catch (UnsupportedEncodingException ex) { 117 throw new IllegalStateException(ex); 118 } 119 } 120 121}