~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/games/strategy/engine/pbem/AllYouCanUploadDotComPBEMMessenger.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
13
 */
 
14
 
 
15
/*
 
16
 * AllYouCanUploadDotComPBEMMessenger.java
 
17
 *
 
18
 *
 
19
 * Created on November 21, 2006, 8:56 PM
 
20
 */
 
21
 
 
22
package games.strategy.engine.pbem;
 
23
 
 
24
import games.strategy.net.MultiPartFormOutputStream;
 
25
import java.io.*;
 
26
import java.net.*;
 
27
import java.util.regex.Matcher;
 
28
import java.util.regex.Pattern;
 
29
 
 
30
/**
 
31
 * @author Tony Clayton
 
32
 * @version 1.0
 
33
 */
 
34
public class AllYouCanUploadDotComPBEMMessenger
 
35
    implements IPBEMScreenshotMessenger
 
36
{
 
37
    private transient String m_screenshotRef = null;
 
38
 
 
39
    public AllYouCanUploadDotComPBEMMessenger()
 
40
    {
 
41
    }
 
42
 
 
43
    public String getName()
 
44
    {
 
45
        return "www.AllYouCanUpload.com";
 
46
    }
 
47
 
 
48
    public boolean getNeedsUsername()
 
49
    {
 
50
        return false;
 
51
    }
 
52
 
 
53
    public boolean getNeedsPassword()
 
54
    {
 
55
        return false;
 
56
    }
 
57
 
 
58
    public boolean getCanViewPosted()
 
59
    {
 
60
        return false;
 
61
    }
 
62
 
 
63
    public void viewPosted()
 
64
    {
 
65
    }
 
66
 
 
67
    public void setGameId(String gameId)
 
68
    {
 
69
    }
 
70
 
 
71
    public String getGameId()
 
72
    {
 
73
        return null;
 
74
    }
 
75
 
 
76
    public void setUsername(String username)
 
77
    {
 
78
    }
 
79
 
 
80
    public String getUsername()
 
81
    {
 
82
        return null;
 
83
    }
 
84
 
 
85
    public void setPassword(String password)
 
86
    {
 
87
    }
 
88
 
 
89
    public String getPassword()
 
90
    {
 
91
        return null;
 
92
    }
 
93
 
 
94
    public boolean postScreenshot(String fileName, InputStream fileIn)
 
95
        throws IOException
 
96
    {
 
97
        URL url = null;
 
98
        URLConnection urlConn = null;
 
99
        MultiPartFormOutputStream out = null;
 
100
 
 
101
        m_screenshotRef = null;
 
102
 
 
103
        // set up connection
 
104
        try
 
105
        {
 
106
            url = new URL("http://allyoucanupload.webshots.com/uploadcomplete");
 
107
        }
 
108
        catch(MalformedURLException e)
 
109
        {
 
110
            e.printStackTrace();
 
111
            return false;
 
112
        }
 
113
        try
 
114
        {
 
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%");
 
125
        }
 
126
        catch(Exception e)
 
127
        {
 
128
            e.printStackTrace();
 
129
            return false;
 
130
        }
 
131
        // this one throws an exception
 
132
        out.writeFile("images[0].fileName", "image/png", fileName, fileIn);
 
133
 
 
134
        // send request to server
 
135
        out.close();
 
136
 
 
137
        int code = ((HttpURLConnection)urlConn).getResponseCode();
 
138
        if(code != 200)
 
139
        {
 
140
            // http error
 
141
            String msg = ((HttpURLConnection)urlConn).getResponseMessage();
 
142
            m_screenshotRef = String.valueOf(code)+": "+msg;
 
143
            return false;
 
144
        }
 
145
        // read response from server
 
146
        try
 
147
        {
 
148
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
 
149
            String line = "";
 
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)
 
153
            {
 
154
                Matcher m = p.matcher(line);
 
155
                if(m.matches())
 
156
                    m_screenshotRef = m.group(1);
 
157
            }
 
158
            in.close();
 
159
        }
 
160
        catch(IOException ioe)
 
161
        {
 
162
            ioe.printStackTrace();
 
163
            return false;
 
164
        }
 
165
        if(m_screenshotRef == null)
 
166
        {
 
167
            m_screenshotRef = "Error: screenshot URL could not be found after posting.";
 
168
            return false;
 
169
        }
 
170
 
 
171
        return true;
 
172
    }
 
173
 
 
174
    public String getScreenshotRef()
 
175
    {
 
176
        return m_screenshotRef;
 
177
    }
 
178
 
 
179
    public String toString()
 
180
    {
 
181
        return getName();
 
182
    }
 
183
}