3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.math
.BigInteger
;
6 import java
.util
.HashMap
;
9 import org
.apache
.commons
.httpclient
.DefaultHttpMethodRetryHandler
;
10 import org
.apache
.commons
.httpclient
.HttpClient
;
11 import org
.apache
.commons
.httpclient
.HttpException
;
12 import org
.apache
.commons
.httpclient
.HttpMethod
;
13 import org
.apache
.commons
.httpclient
.HttpStatus
;
14 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
15 import org
.apache
.commons
.httpclient
.params
.HttpMethodParams
;
17 public class DAAPUtilities
{
19 private HttpClient client
;
20 private HttpClient clientSong
;
22 protected DAAPUtilities(final String hostname
, final int port
) throws IOException
{
24 client
= new HttpClient();
25 clientSong
= new HttpClient();
27 this.names
= new HashMap
<Integer
,String
>();
28 this.types
= new HashMap
<Integer
,Short
>();
30 //this.requests = new HashMap<InputStream, HttpMethod>();
32 DAAPUtilities
.initContentCodes(this.names
, this.types
);
34 this.retrieveContentCodes(hostname
, port
);
37 private InputStream
request(HttpClient client
, String hostname
, int port
, String request
) throws IOException
{
38 String requestURI
= "http://" + hostname
+ ":" + port
+ "/" + request
;
40 HttpMethod method
= new GetMethod(requestURI
);
42 InputStream responseBody
= null;
44 // Provide custom retry handler is necessary
45 method
.getParams().setParameter(HttpMethodParams
.RETRY_HANDLER
,
46 new DefaultHttpMethodRetryHandler(3, false));
49 // Execute the method.
50 int statusCode
= client
.executeMethod(method
);
52 if (statusCode
/100 != HttpStatus
.SC_OK
/100) {
53 System
.err
.println("Method failed: " + method
.getStatusLine());
56 // Read the response body.
57 responseBody
= method
.getResponseBodyAsStream();
59 //this.requests.put(responseBody, method);
63 } catch (HttpException e
) {
64 System
.err
.println("Fatal protocol violation: " + e
.getMessage());
65 //e.printStackTrace();
71 protected InputStream
request(String hostname
, int port
, String request
) throws IOException
{
72 return request(client
, hostname
, port
, request
);
75 protected InputStream
songRequest(String hostname
, int port
, String request
) throws IOException
{
76 return request(clientSong
, hostname
, port
, request
);
79 protected void release(InputStream request) {
80 if ((request != null) && (this.requests.get(request) != null)) {
83 } catch (IOException e) {
86 this.requests.get(request).releaseConnection();
87 this.requests.remove(request);
91 private void retrieveContentCodes(final String hostname
, int port
) throws IOException
{
93 InputStream response
= this.request(hostname
, port
, "content-codes");
95 if (response
== null) {
96 throw new NullPointerException();
99 DAAPEntry entry
= DAAPEntry
.parseStream(response
, this.types
);
101 if (entry
.getName() == stringToInt("mccr")) {
102 for (DAAPEntry e
: entry
) {
104 if (e
.getName() != stringToInt("mdcl")) {
108 Map
<Integer
,Object
> values
= e
.getValueMap();
110 String name
= (String
)values
.get(stringToInt("mcna"));
111 short type
= (Short
)values
.get(stringToInt("mcty"));
112 int number
= (Integer
)values
.get(stringToInt("mcnm"));
114 this.names
.put(number
, name
);
115 this.types
.put(number
, type
);
119 //this.release(response);
122 protected Map
<Integer
, String
> names
;
123 protected Map
<Integer
, Short
> types
;
125 //private Map<InputStream, HttpMethod> requests;
127 private static void initContentCodes(Map
<Integer
, String
> names
, Map
<Integer
, Short
> types
) {
128 names
.put(stringToInt("mdcl"), "dmap.dictionary");
129 types
.put(stringToInt("mdcl"), DAAPEntry
.LIST
);
130 names
.put(stringToInt("mstt"), "dmap.status");
131 types
.put(stringToInt("mstt"), DAAPEntry
.INTEGER
);
132 names
.put(stringToInt("mcnm"), "dmap.contentcodesnumber");
133 types
.put(stringToInt("mcnm"), DAAPEntry
.INTEGER
);
134 names
.put(stringToInt("mcty"), "dmap.contentcodestype");
135 types
.put(stringToInt("mcty"), DAAPEntry
.SHORT
);
136 names
.put(stringToInt("mcna"), "dmap.contentcodesname");
137 types
.put(stringToInt("mcna"), DAAPEntry
.STRING
);
138 names
.put(stringToInt("mccr"), "dmap.contentcodes");
139 types
.put(stringToInt("mccr"), DAAPEntry
.LIST
);
140 names
.put(stringToInt("msrv"), "dmap.serverinforesponse");
141 types
.put(stringToInt("msrv"), DAAPEntry
.LIST
);
144 public static int stringToInt(String name
) {
145 return new BigInteger(name
.getBytes()).intValue();
148 public static String
intToString(int name
) {
149 byte[] bytes
= new byte[4];
151 for (int i
= 0; i
< 4; i
++) {
152 bytes
[3-i
] = (byte)(n
% 256);
155 return new String(bytes
);