summaryrefslogtreecommitdiffstats
path: root/sandbox/mobile-android/android-jdk-classes/src/org/apache/tuscany/sca/android/DexURLConnection.java
blob: af0e93b043eac6648d0161798a9c1ad892dc7de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package org.apache.tuscany.sca.android;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownServiceException;

import android.content.Context;

public class DexURLConnection extends URLConnection {

	private InputStream input;

	protected DexURLConnection(URL url) {
		super(url);
		setAllowUserInteraction(false);
		setUseCaches(false);
		setDefaultUseCaches(false);
		setConnectTimeout(0);
		setReadTimeout(0);
		setDoInput(true);
		setDoOutput(false);

	}

	public InputStream getInputStream() throws IOException {

		if (input == null) {
			connect();
		}

		return input;

	}
	
	private String guessContentTypeFromInput() {
		
		if (!connected) {
			
			try {
				connect();
			} catch (IOException e) {
				return null;
			}
			
		}
		
		try {
			return guessContentTypeFromStream(input);
		} catch (IOException e) {
			return null;
		}
		
	}
	
	@Override
	public String getContentType() {
		
		if (DexResource.getFolder(url.getPath()) == null || DexResource.getFile(url.getPath()) == null) {
			return "application/x-dex";
		}
		
		return guessContentTypeFromInput();
		
	}

	public OutputStream getOutputStream() throws IOException {
		throw new UnknownServiceException("Output not supported!");
	}

	public void connect() throws IOException {
		
		if (!connected) {
			String host = url.getHost();
			Context[] contexts = ContextRegistry.getContexts(host);
			
			if (contexts.length == 0) {
				throw new IOException("Android context not found!");
			}
			
			Context context = contexts[0];
			
			if ("".equals(host)) {
				throw new IOException("not valid host name: \"\"");
			}
			
			String path = url.getPath();
			String file = DexResource.getFile(path);
			String folder = DexResource.getFolder(path);
			
			if (file == null) {
				return;
			}
			
			file = file.replace('.', '_');
			
			try {
				
				StringBuffer sb = new StringBuffer(context.getPackageName());
				sb.append('.').append('R').append('$').append(folder);
				
				Class clazz = getClass().getClassLoader().loadClass(sb.toString());
				Field field = clazz.getDeclaredField(file);
				
				int id = field.getInt(null);
				input = context.getResources().openRawResource(id);
				connected = true;
				
			} catch (ClassNotFoundException e) {
				throw new IOException(e.getMessage());
			} catch (SecurityException e) {
				throw new IOException(e.getMessage());
			} catch (NoSuchFieldException e) {
				throw new IOException(e.getMessage());
			} catch (IllegalArgumentException e) {
				throw new IOException(e.getMessage());
			} catch (IllegalAccessException e) {
				throw new IOException(e.getMessage());
			}
			
		}

	}

}