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 17 package com.android.bluetooth.mapclient; 18 19 /*import android.bluetooth.client.map.Bmessage; 20 import android.bluetooth.client.map.BmessageParser; 21 import android.bluetooth.client.map.Client.CharsetType; 22 import android.bluetooth.client.map.utils.ObexAppParameters; 23 */ 24 25 import android.util.Log; 26 27 import java.io.ByteArrayOutputStream; 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.io.UnsupportedEncodingException; 31 import java.nio.charset.StandardCharsets; 32 33 import javax.obex.ClientSession; 34 import javax.obex.HeaderSet; 35 import javax.obex.ResponseCodes; 36 37 final 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 public String getHandle() { 102 try { 103 return (String) mHeaderSet.getHeader(HeaderSet.NAME); 104 } catch (IOException e) { 105 Log.e(TAG, "Unexpected exception while reading handle!", e); 106 return null; 107 } 108 } 109 110 @Override 111 public void execute(ClientSession session) throws IOException { 112 executeGet(session); 113 } 114 } 115