1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.bluetooth.client.map;
18
19import android.util.Log;
20
21
22import android.bluetooth.client.map.BluetoothMasClient.CharsetType;
23import android.bluetooth.client.map.utils.ObexAppParameters;
24
25import java.io.ByteArrayOutputStream;
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.UnsupportedEncodingException;
29import java.nio.charset.StandardCharsets;
30
31import javax.obex.ClientSession;
32import javax.obex.HeaderSet;
33import javax.obex.ResponseCodes;
34
35final class BluetoothMasRequestGetMessage extends BluetoothMasRequest {
36
37    private static final String TAG = "BluetoothMasRequestGetMessage";
38
39    private static final String TYPE = "x-bt/message";
40
41    private BluetoothMapBmessage mBmessage;
42
43    public BluetoothMasRequestGetMessage(String handle, CharsetType charset, boolean attachment) {
44
45        mHeaderSet.setHeader(HeaderSet.NAME, handle);
46
47        mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);
48
49        ObexAppParameters oap = new ObexAppParameters();
50
51        oap.add(OAP_TAGID_CHARSET, CharsetType.UTF_8.equals(charset) ? CHARSET_UTF8
52                : CHARSET_NATIVE);
53
54        oap.add(OAP_TAGID_ATTACHMENT, attachment ? ATTACHMENT_ON : ATTACHMENT_OFF);
55
56        oap.addToHeaderSet(mHeaderSet);
57    }
58
59    @Override
60    protected void readResponse(InputStream stream) {
61
62        ByteArrayOutputStream baos = new ByteArrayOutputStream();
63        byte[] buf = new byte[1024];
64
65        try {
66            int len;
67            while ((len = stream.read(buf)) != -1) {
68                baos.write(buf, 0, len);
69            }
70        } catch (IOException e) {
71            Log.e(TAG, "I/O exception while reading response", e);
72        }
73
74        // Convert the input stream using UTF-8 since the attributes in the payload are all encoded
75        // according to it. The actual message body may need to be transcoded depending on
76        // charset/encoding defined for body-content.
77        String bmsg;
78        try {
79            bmsg = baos.toString(StandardCharsets.UTF_8.name());
80        } catch (UnsupportedEncodingException ex) {
81            Log.e(TAG,
82                "Coudn't decode the bmessage with UTF-8. Something must be really messed up.");
83            return;
84        }
85
86        mBmessage = BluetoothMapBmessageParser.createBmessage(bmsg);
87
88        if (mBmessage == null) {
89            mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
90        }
91    }
92
93    public BluetoothMapBmessage getMessage() {
94        return mBmessage;
95    }
96
97    @Override
98    public void execute(ClientSession session) throws IOException {
99        executeGet(session);
100    }
101}
102