2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
6
* This program is distributed in the hope that it will be useful,
7
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
* GNU General Public License for more details.
10
* You should have received a copy of the GNU General Public License
11
* along with this program; if not, write to the Free Software
12
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
* AllYouCanUploadDotComPBEMMessenger.java
19
* Created on November 21, 2006, 8:56 PM
22
package games.strategy.engine.pbem;
24
import games.strategy.net.MultiPartFormOutputStream;
27
import java.util.regex.Matcher;
28
import java.util.regex.Pattern;
31
* @author Tony Clayton
34
public class AllYouCanUploadDotComPBEMMessenger
35
implements IPBEMScreenshotMessenger
37
private transient String m_screenshotRef = null;
39
public AllYouCanUploadDotComPBEMMessenger()
43
public String getName()
45
return "www.AllYouCanUpload.com";
48
public boolean getNeedsUsername()
53
public boolean getNeedsPassword()
58
public boolean getCanViewPosted()
63
public void viewPosted()
67
public void setGameId(String gameId)
71
public String getGameId()
76
public void setUsername(String username)
80
public String getUsername()
85
public void setPassword(String password)
89
public String getPassword()
94
public boolean postScreenshot(String fileName, InputStream fileIn)
98
URLConnection urlConn = null;
99
MultiPartFormOutputStream out = null;
101
m_screenshotRef = null;
106
url = new URL("http://allyoucanupload.webshots.com/uploadcomplete");
108
catch(MalformedURLException e)
115
String boundary = MultiPartFormOutputStream.createBoundary();
116
urlConn = MultiPartFormOutputStream.createConnection(url);
117
urlConn.setRequestProperty("Accept", "*/*");
118
urlConn.setRequestProperty("Content-Type", MultiPartFormOutputStream.getContentType(boundary));
119
urlConn.setRequestProperty("Connection", "Keep-Alive");
120
urlConn.setRequestProperty("Cache-Control", "no-cache");
121
((HttpURLConnection)urlConn).setInstanceFollowRedirects(true);
122
out = new MultiPartFormOutputStream(urlConn.getOutputStream(), boundary);
123
out.writeField("imagesCount", "1");
124
out.writeField("images[0].submittedPhotoSize", "100%");
131
// this one throws an exception
132
out.writeFile("images[0].fileName", "image/png", fileName, fileIn);
134
// send request to server
137
int code = ((HttpURLConnection)urlConn).getResponseCode();
141
String msg = ((HttpURLConnection)urlConn).getResponseMessage();
142
m_screenshotRef = String.valueOf(code)+": "+msg;
145
// read response from server
148
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
150
// server will always serve images in .jpg format
151
Pattern p = Pattern.compile(".*?<input type=\"text\" .*?value=\"(http://.*?jpg)\">.*");
152
while ((line = in.readLine()) != null)
154
Matcher m = p.matcher(line);
156
m_screenshotRef = m.group(1);
160
catch(IOException ioe)
162
ioe.printStackTrace();
165
if(m_screenshotRef == null)
167
m_screenshotRef = "Error: screenshot URL could not be found after posting.";
174
public String getScreenshotRef()
176
return m_screenshotRef;
179
public String toString()