RequestGetMessage.java revision b68b83c0463161ce65922ec606f12244446efab1
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 com.android.bluetooth.mapclient;
18
19/*import android.bluetooth.client.map.Bmessage;
20import android.bluetooth.client.map.BmessageParser;
21import android.bluetooth.client.map.Client.CharsetType;
22import android.bluetooth.client.map.utils.ObexAppParameters;
23*/
24
25import android.util.Log;
26
27import java.io.ByteArrayOutputStream;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.UnsupportedEncodingException;
31import java.nio.charset.StandardCharsets;
32
33import javax.obex.ClientSession;
34import javax.obex.HeaderSet;
35import javax.obex.ResponseCodes;
36
37final class RequestGetMessage extends Request {
38
39    private static final String TAG = "RequestGetMessage";
40
41    private static final String TYPE = "x-bt/message";
42
43    private Bmessage mBmessage;
44
45    public RequestGetMessage(String handle, MasClient.CharsetType charset,
46            boolean attachment) {
47
48        mHeaderSet.setHeader(HeaderSet.NAME, handle);
49
50        mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);
51
52        ObexAppParameters oap = new ObexAppParameters();
53
54        oap.add(OAP_TAGID_CHARSET,
55                MasClient.CharsetType.UTF_8.equals(charset) ? CHARSET_UTF8
56                        : CHARSET_NATIVE);
57
58        oap.add(OAP_TAGID_ATTACHMENT, attachment ? ATTACHMENT_ON : ATTACHMENT_OFF);
59
60        oap.addToHeaderSet(mHeaderSet);
61    }
62
63    @Override
64    protected void readResponse(InputStream stream) {
65
66        ByteArrayOutputStream baos = new ByteArrayOutputStream();
67        byte[] buf = new byte[1024];
68
69        try {
70            int len;
71            while ((len = stream.read(buf)) != -1) {
72                baos.write(buf, 0, len);
73            }
74        } catch (IOException e) {
75            Log.e(TAG, "I/O exception while reading response", e);
76        }
77
78        // Convert the input stream using UTF-8 since the attributes in the payload are all encoded
79        // according to it. The actual message body may need to be transcoded depending on
80        // charset/encoding defined for body-content.
81        String bmsg;
82        try {
83            bmsg = baos.toString(StandardCharsets.UTF_8.name());
84        } catch (UnsupportedEncodingException ex) {
85            Log.e(TAG,
86                    "Coudn't decode the bmessage with UTF-8. Something must be really messed up.");
87            return;
88        }
89
90        mBmessage = BmessageParser.createBmessage(bmsg);
91
92        if (mBmessage == null) {
93            mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
94        }
95    }
96
97    public Bmessage getMessage() {
98        return mBmessage;
99    }
100
101    @Override
102    public void execute(ClientSession session) throws IOException {
103        executeGet(session);
104    }
105}
106